dev-resources.site
for different kinds of informations.
Multiple page Vite on Github Pages with HTML5 History Mode
Published at
4/20/2022
Categories
vite
vue
webdev
github
Author
Pacharapol Withayasakpunt
Indeed, one of the way is simply pre-render (e.g. with vite-plugin-ssr).
Nonetheless, it is possible to enable fallback (without redirect settings, which doesn't exist in Github Pages), if you don't want to pre-render, and sacrifice JavaScript-ness yet. The way is, to clone dist/index.html
to dist/sub/route/index.html
. (SSG's also have multple **/index.html
.)
import { existsSync, mkdirSync, copyFileSync } from 'fs'
import { resolve as pathResolve, join as pathJoin } from 'path'
function cloneIndexHtmlPlugin(routes: string[] = []): PluginOption {
const name = 'CloneIndexHtmlPlugin'
const outDir = 'dist' // config's `build.outDir`
const src = pathJoin(outDir, 'index.html')
return {
name,
closeBundle: () => {
// routes.push(...)
routes.map((p) => {
const dir = pathResolve(outDir, p)
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true })
}
const dst = pathJoin(outDir, p, 'index.html')
// It is possible to edit HTML here, too.
copyFileSync(src, dst)
console.log(`${name}: Copied ${src} to ${dst}`)
})
},
}
}
And put the plugin in vite.config.ts
// https://vitejs.dev/config/
export default {
base: `/${GITHUB_REPO_NAME}/`
plugins: [cloneIndexHtmlPlugin()],
}
Integrating with Vue 3 and Vue Router 4
VueRouter
makes navigating easier, without full page reload; however, it does need a little extra config - baseURL
.
import { createRouter, createWebHistory } from 'vue-router'
export const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
// Define your routes here
],
})
Route names can be glob dynamically with import.meta.glob
.
import { createRouter, createWebHistory } from 'vue-router'
export const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
// Does not allow string templating - `${PAGE_DIR}/**/*${PAGE_EXT}` does not work.
...Object.entries(import.meta.glob('../pages/**/*.vue')).map(
([p, component]) => {
const PAGE_DIR = '../pages'
const PAGE_EXT = '.vue'
const PAGE_INDEX = 'index'
let path = p.substring(PAGE_DIR.length, p.length - PAGE_EXT.length)
if (path.endsWith(PAGE_INDEX)) {
path = path.substring(0, path.length - PAGE_INDEX.length - 1)
}
if (!path) {
path = '/'
}
return {
path,
component,
}
},
),
],
})
cloneIndexHtmlPlugin
should also glob - fast-glob
is what import.meta.glob
uses, so copy it.
import { sync as glob } from 'fast-glob'
// ...
const PAGE_EXT = '.vue'
const PAGE_INDEX = 'index'
routes.push(
...glob(`**/*${PAGE_EXT}`, {
cwd: 'src/pages',
})
.map((p) => {
p = p.substring(0, p.length - PAGE_EXT.length)
if (p.endsWith('/' + PAGE_INDEX)) {
p = p.substring(0, p.length - PAGE_INDEX.length - 1)
}
return p
})
.filter((p) => p !== PAGE_INDEX),
)
Articles
12 articles in total
Multiple page Vite on Github Pages with HTML5 History Mode
currently reading
Why do I even need favicon to prevent server errors? And, why the default is .ico?
read article
Do we need two version-locking files? (Pipfile = "*" vs pypoetry.toml vs venv + requirements.txt)
read article
Customizable PDF viewer with self-built PDF.js
read article
`docker compose` (v2) vs `docker-compose` (v1) vs `podman-compose` - which one to choose?
read article
Debugging in Rust
read article
What are some Linux distros you spent significant time on?
read article
Noticeable differences between podman and docker
read article
VSCode word wrap for very very long base64 image datauri? (Generate no more than 5 lines.)
read article
How do you calculate 3^3^3^3? (What algo / struct?)
read article
Does Google Docs really support non-English speakers? (Thai, specifically) And, any alternatives?
read article
Please ELI5 what Parquet is for, and NOT for
read article
Featured ones: