Skip to content

Commit

Permalink
Move Netlify redirects to Nuxt redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Siilwyn committed Jan 3, 2025
1 parent 16cd029 commit 8ce48b3
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 29 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
21 changes: 0 additions & 21 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,3 @@ for = "/*"
Strict-Transport-Security = "max-age=63072000; includeSubDomains; preload"
Referrer-Policy = "no-referrer-when-downgrade"
X-Frame-Options = "SAMEORIGIN"

[[redirects]]
from = "/"
to = "/nl/"
status = 302
conditions = { Language = ["nl"] }

[[redirects]]
from = "/"
to = "/en/"
status = 302

[[redirects]]
from = "/blog-feed.xml"
to = "/blog/feed.json"
status = 301

[[redirects]]
from = "/mogelijk/api/event"
to = "https://plausible.io/api/event"
status = 200
5 changes: 5 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ export default defineNuxtConfig({
'@/components/app-core/index.css',
],
nitro: {
preset: 'netlify-edge',
prerender: {
crawlLinks: false,
},
routeRules: {
'/': { prerender: false },
'/blog-feed.xml': {
redirect: { to: '/blog/feed.json', statusCode: 301 },
},
'/mogelijk/api/event': { proxy: { to: 'https://plausible.io/api/event' } },
},
},
runtimeConfig: {
Expand Down
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"private": true,
"scripts": {
"dev": "nuxt dev",
"build": "nuxt generate",
"build": "nuxt build",
"lint": "eslint '**/*.vue'",
"lint-staged": "nano-staged",
"prepare": "husky install"
},
"dependencies": {
"@json-feed-types/1_1": "^1.0.2",
"@nuxtjs/plausible": "^0.2.0",
"@std/http": "npm:@jsr/std__http@^1.0.10",
"@voorhoede/vue-dato-video": "^3.1.0",
"datocms-listen": "^0.1.14",
"datocms-structured-text-utils": "^2.0.4",
Expand Down
10 changes: 5 additions & 5 deletions src/components/language-switcher/language-switcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<script setup>
const i18nSlugs = await useI18nSlugs();
const route = useRoute();
import { cookieName } from '~/lib/i18n.js';
const getLocaleRoute = (code) => {
const isDynamicRoute = Boolean(i18nSlugs.value)
Expand All @@ -52,14 +53,13 @@ const getLocaleRoute = (code) => {
const saveLocale = (code) => {
const cookie = document.cookie;
const langKey = "nf_lang"; // @See https://www.netlify.com/docs/redirects/#geoip-and-language-based-redirects
if (cookie.match(new RegExp(langKey) !== null)) {
if (cookie.match(new RegExp(cookieName) !== null)) {
document.cookie = cookie.replace(
new RegExp(`${langKey}=[A-Za-z-]+;`),
`${langKey}=${code};`
new RegExp(`${cookieName}=[A-Za-z-]+;`),
`${cookieName}=${code};`
);
} else {
document.cookie = `${langKey}=${code}; path=/; ${cookie}`;
document.cookie = `${cookieName}=${code}; path=/; ${cookie}`;
}
};
</script>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/i18n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const defaultLanguage = 'en';
export const cookieName = 'chosen-language';

export const locales = [
{
code: 'en',
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import rosetta from 'rosetta';
import { joinURL, withTrailingSlash } from 'ufo';
import { locales } from '../lib/i18n';
import { locales, defaultLanguage } from '../lib/i18n';
import messages from '../../.cache/ui-translations.json';

const i18n = rosetta(messages);
const defaultLanguage = 'en';

export default defineNuxtPlugin((nuxtApp) => {
i18n.locale(nuxtApp._route.params.language);
Expand Down
28 changes: 28 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { acceptsLanguages } from '@std/http/negotiation';
import { cookieName, defaultLanguage, locales } from '~/lib/i18n.js';

const localeCodes = locales.map(({ code }) => code);

export default defineEventHandler((event) => {
const request = toWebRequest(event);
const chosenLanguage = getCookie(event, cookieName);

if (localeCodes.includes(chosenLanguage)) {
return sendRedirect(
event,
`/${chosenLanguage}/`,
302,
);
}

const clientLocale = acceptsLanguages(
request,
...localeCodes,
);

sendRedirect(
event,
`/${clientLocale || defaultLanguage}/`,
302,
);
});
3 changes: 3 additions & 0 deletions src/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../.nuxt/tsconfig.server.json"
}

0 comments on commit 8ce48b3

Please sign in to comment.