Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to SSG from SSR on Cloudflare Pages/Workers #225

Merged
merged 9 commits into from
Oct 1, 2024
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# Project-specific
#
/dataset/
/functions/
/public/dataset/
/public/_redirects

#
# System
Expand Down Expand Up @@ -59,6 +61,8 @@ yarn-error.log*
# When nitro.preset: "cloudflare-pages" is set in nuxt.config.ts,
# build output is placed in dist/
/dist/
# dist/ directory is generated as a symlink when you run `nuxt generate`
/dist

# npm is the package manager for this project.
yarn.lock
Expand Down
61 changes: 61 additions & 0 deletions cloudflare/functions/locale-redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { parse } from "@escapace/accept-language-parser";

type AvailableLocale = "en" | "ja" | "zh-CN";

const redirectByLanguage: PagesFunction = async ({ request }) => {
const getUserLanguage = (acceptLanguage: string): AvailableLocale => {
const languages = parse(acceptLanguage);

for (const language of languages) {
if (language.code === "ja") {
return "ja";
} else if (language.code === "zh") {
return "zh-CN";
} else {
return "en";
}
}

// If acceptLanguage is not sent from client
return "en";
};

const reqURL = new URL(request.url);
const baseURL = `${ reqURL.protocol }//${ reqURL.host }`;
const rawPath = reqURL.searchParams.get("path");
const path = rawPath ? decodeURIComponent(rawPath) : undefined;

if (!path) {
// This should not happen by normal usecase unless there is a bug
const destURL = new URL("/en", baseURL);
destURL.searchParams.append("why", "path-not-given");
return Response.redirect(destURL.href, 302);
}

if (!path.startsWith("/")) {
// This should not happen by normal usecase unless there is a bug
const destURL = new URL("/en", baseURL);
destURL.searchParams.append("why", "not-a-path");
destURL.searchParams.append("givenpath", encodeURIComponent(path));
return Response.redirect(destURL.href, 302);
}

if (
path === "/en" || path.startsWith("/en/")
|| path === "/ja" || path.startsWith("/ja/")
|| path === "/zh-CN" || path.startsWith("/zh-CN/")
) {
// This should not happen by normal usecase unless there is a bug
const destURL = new URL(path, baseURL);
destURL.searchParams.append("why", "already-locale-given");
return Response.redirect(destURL.href, 302);
} else {
const acceptLanguage = request.headers.get("Accept-Language") as string;
const userLanguage = getUserLanguage(acceptLanguage);

const destURL = new URL(`/${ userLanguage + path }`, baseURL);
return Response.redirect(destURL.href, 302);
}
};

export const onRequestGet: PagesFunction = redirectByLanguage;
29 changes: 0 additions & 29 deletions middleware/redirects.global.ts

This file was deleted.

5 changes: 0 additions & 5 deletions middleware/trailing-slash.global.ts

This file was deleted.

17 changes: 7 additions & 10 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ const isLocal = !process.env.SERVER_ENV || process.env.SERVER_ENV === "local";

export default defineNuxtConfig({
compatibilityDate: "2024-07-31",
ssr: true,
ssr: true, // Enable prerender
components: true,

nitro: {
preset: "cloudflare-pages",
prerender: {
autoSubfolderIndex: false,
},
},

typescript: {
strict: true,
tsConfig: {
Expand All @@ -34,6 +37,7 @@ export default defineNuxtConfig({
"../public/dataset/**",
// Cloudflare
"../.wrangler/**",
"../functions/**",
],
},
},
Expand All @@ -45,7 +49,6 @@ export default defineNuxtConfig({
},

modules: [
"nitro-cloudflare-dev",
"@nuxtjs/i18n",
"@nuxtjs/robots",
"@nuxtjs/sitemap",
Expand All @@ -72,14 +75,8 @@ export default defineNuxtConfig({
},
],
strategy: "prefix",
defaultLocale: "en",
baseUrl: "https://genshin-dictionary.com",
detectBrowserLanguage: {
useCookie: true,
cookieSecure: true,
fallbackLocale: "en",
redirectOn: "no prefix",
},
detectBrowserLanguage: false,
},

sitemap: {
Expand Down
Loading