Skip to content

Commit

Permalink
Support SSG
Browse files Browse the repository at this point in the history
  • Loading branch information
encode42 committed Jan 17, 2023
1 parent 939747c commit 36a435f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 3 additions & 1 deletion adaptors/cloudflare-pages/vite.config.ts
Expand Up @@ -14,7 +14,9 @@ export default extendConfig(baseConfig, () => {
}
},
"plugins": [
cloudflarePagesAdaptor()
cloudflarePagesAdaptor({
"staticGenerate": true
})
]
};
});
2 changes: 1 addition & 1 deletion src/route/index.tsx → src/route/[...lang]/index.tsx
Expand Up @@ -7,7 +7,7 @@ import { config } from "~/data/config";
import { $translate as t, Speak } from "qwik-speak";
import { ChangeLocale } from "~/component/change-locale/change-locale";
import { ChangeColorScheme } from "~/component/change-color-scheme/change-color-scheme";
import Layout from "~/route/layout";
import Layout from "~/route/[...lang]/layout";
import { FileName } from "~/component/config/file-name/file-name";
import { Flags } from "~/component/config/flags/flags";
import { Memory } from "~/component/config/memory/memory";
Expand Down
15 changes: 12 additions & 3 deletions src/route/layout.tsx → src/route/[...lang]/layout.tsx
Expand Up @@ -6,10 +6,10 @@ import { config } from "~/speak-config";
import { ColorScheme } from "~/context/color-scheme/wrapper";
import supportedLocales from "~/generated/supportedLocales.json";

export const onRequest: RequestHandler = ({ headers, locale, cookie }) => {
export const onRequest: RequestHandler = ({ url, headers, locale, cookie, params, redirect }) => {
const langCookie = cookie.get("lang");

let lang: string | undefined = langCookie?.value;
let lang: string | undefined = params.lang || langCookie?.value;
if (!lang) {
const acceptLanguage = headers.get("accept-language");

Expand All @@ -20,9 +20,18 @@ export const onRequest: RequestHandler = ({ headers, locale, cookie }) => {
lang = acceptLanguage.split("-")[0];
}
}

if (lang && !supportedLocales.includes(lang)) {
lang = undefined;
}
}
lang ??= config.defaultLocale.lang;

if ((langCookie && lang !== config.defaultLocale.lang) && lang !== params.lang) {
throw redirect(302, `/${lang}${url.pathname}`);
}

locale(lang ?? config.defaultLocale.lang);
locale(lang);
};

export const getColorScheme = loader$<unknown, ColorSchemes | undefined>(({ cookie }) => {
Expand Down
18 changes: 7 additions & 11 deletions src/speak-config.ts
Expand Up @@ -11,22 +11,18 @@ export const config: SpeakConfig = {
"lang": locale
})),
"assets": [
"app",
"runtime"
"app"
]
};

export const loadTranslation$: LoadTranslationFn = $(async (lang: string, asset: string, origin?: string) => {
if (import.meta.env.DEV || asset === "runtime") {
let url = "";
if (isServer && origin) {
url = origin;
}
url += `/i18n/${lang}/${asset}.json`;

const data = await fetch(url);
return data.json();
let url = "";
if (isServer && origin) {
url = origin;
}
url += `/i18n/${lang}/${asset}.json`;
const data = await fetch(url);
return data.json();
});

export const translationFn: TranslationFn = {
Expand Down

0 comments on commit 36a435f

Please sign in to comment.