Skip to content

Commit

Permalink
feat(plugin-redirect): improve autoLocale homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 5, 2024
1 parent 89180d4 commit 0d63fb4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 73 deletions.
74 changes: 3 additions & 71 deletions plugins/plugin-redirect/src/node/ensureRootHomePage.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,18 @@
import { removeEndingSlash } from '@vuepress/helper'
import type { App } from 'vuepress/core'
import { createPage } from 'vuepress/core'
import type { RedirectLocaleConfig } from '../shared/index.js'

export const ensureRootHomePage = async (
app: App,
localeOptions: RedirectLocaleConfig,
): Promise<void> => {
const {
options: { base },
pages,
} = app

export const ensureRootHomePage = async (app: App): Promise<void> => {
if (
// root homepage not exists
pages.every(({ path }) => path !== '/')
app.pages.every(({ path }) => path !== '/')
) {
const availableLocales = pages
.filter(({ pathLocale, path }) => pathLocale === path)
.map(({ pathLocale }) => pathLocale)

pages.push(
app.pages.push(
await createPage(app, {
path: '/',
frontmatter: { title: 'Home' },
// set markdown content
content: `\
Redirecting to the correct locale...
<script setup>
import { onMounted } from "vue";
if(!__VUEPRESS_DEV__)
onMounted(() => {
const { languages } = window.navigator;
const { hash, origin } = window.location;
const anchor = hash.substring(1);
const localeConfig = ${JSON.stringify(localeOptions.localeConfig)};
const availableLocales = ${JSON.stringify(availableLocales)};
const defaultLocale = availableLocales.pop();
let matchedLocalePath = null;
// get matched locale
findLanguage:
for (const lang of languages)
for (const [localePath, langs] of Object.entries(localeConfig))
if (langs.includes(lang)) {
${
localeOptions.localeFallback
? `\
if (!availableLocales.includes(localePath))
continue;
`
: ``
}\
matchedLocalePath = localePath;
break findLanguage;
}
// default link
const defaultLink = defaultLocale? \`\${origin}${removeEndingSlash(
base,
)}\${defaultLocale}\${anchor? \`#\${anchor}\`: ""}\`: null;
// a locale homepage exists
if (matchedLocalePath && availableLocales.includes(matchedLocalePath)) {
location.href = \`\${origin}${removeEndingSlash(
base,
)}\${matchedLocalePath}\${anchor? \`#\${anchor}\`: ""}\`;
}
// we have a default page
else if (defaultLink) {
location.href = defaultLink;
}
// no homepage? WTF, just go to 404
else {
location.href = \`\${origin}${removeEndingSlash(base)}/404.html\`;
}
})
</script>
`,
}),
)
Expand Down
4 changes: 2 additions & 2 deletions plugins/plugin-redirect/src/node/redirectPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export const redirectPlugin =

if (app.env.isDebug) logger.info('Redirect Map:', redirectMap)

if (redirectLocaleConfig.autoLocale)
await ensureRootHomePage(app, redirectLocaleConfig)
if (redirectLocaleConfig.autoLocale && app.env.isDebug)
await ensureRootHomePage(app)
},

onPrepared: async (app): Promise<void> => {
Expand Down

0 comments on commit 0d63fb4

Please sign in to comment.