From 7a29487607abffcc287d93dc55bfff0a9312956c Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Fri, 18 Aug 2023 23:34:58 +0545 Subject: [PATCH] site: quicker redirects to appease the SEO deities --- .../src/routes/docs.html/+server.js | 2 + sites/svelte.dev/src/routes/docs/+page.svelte | 216 ------------------ sites/svelte.dev/src/routes/docs/+server.js | 12 + sites/svelte.dev/src/routes/docs/redirect.js | 58 +++++ 4 files changed, 72 insertions(+), 216 deletions(-) create mode 100644 sites/svelte.dev/src/routes/docs.html/+server.js delete mode 100644 sites/svelte.dev/src/routes/docs/+page.svelte create mode 100644 sites/svelte.dev/src/routes/docs/+server.js create mode 100644 sites/svelte.dev/src/routes/docs/redirect.js diff --git a/sites/svelte.dev/src/routes/docs.html/+server.js b/sites/svelte.dev/src/routes/docs.html/+server.js new file mode 100644 index 0000000000..e036e3cfce --- /dev/null +++ b/sites/svelte.dev/src/routes/docs.html/+server.js @@ -0,0 +1,2 @@ +export const prerender = true; +export { GET } from '../docs/+server.js'; diff --git a/sites/svelte.dev/src/routes/docs/+page.svelte b/sites/svelte.dev/src/routes/docs/+page.svelte deleted file mode 100644 index 3dec6b3a56..0000000000 --- a/sites/svelte.dev/src/routes/docs/+page.svelte +++ /dev/null @@ -1,216 +0,0 @@ - diff --git a/sites/svelte.dev/src/routes/docs/+server.js b/sites/svelte.dev/src/routes/docs/+server.js new file mode 100644 index 0000000000..19b3acd3e6 --- /dev/null +++ b/sites/svelte.dev/src/routes/docs/+server.js @@ -0,0 +1,12 @@ +import js from './redirect.js?raw'; + +// prerenderer will choke otherwise +export const prerender = false; + +export function GET() { + return new Response(``, { + headers: { + 'content-type': 'text/html' + } + }); +} diff --git a/sites/svelte.dev/src/routes/docs/redirect.js b/sites/svelte.dev/src/routes/docs/redirect.js new file mode 100644 index 0000000000..4f4c63c0fe --- /dev/null +++ b/sites/svelte.dev/src/routes/docs/redirect.js @@ -0,0 +1,58 @@ +/** @type {[RegExp, string][]}*/ +const pages_regex_map = [ + // Basic ones + [/(before-we-begin|getting-started)$/i, 'introduction'], + [/template-syntax$/i, 'basic-markup'], + [/component-format$/i, 'svelte-components'], + [/run-time$/i, 'svelte'], + [/compile-time$/i, 'svelte-compiler'], + [/(accessibility-warnings)$/i, '$1'], + + // component-format- + [/component-format-(script|style|script-context-module)$/i, 'svelte-components#$1'], + [/component-format-(script)(?:-?(.*))$/i, 'svelte-components#$1-$2'], + + // template-syntax + [/template-syntax-((?:element|component)-directives)-?(.*)/i, '$1#$2'], + [/template-syntax-slot$/i, 'special-elements#slot'], + [/template-syntax-(slot)-?(.*)/i, 'special-elements#$1-$2'], + [/template-syntax-(if|each|await|key)$/i, 'logic-blocks#$1'], + [/template-syntax-(const|debug|html)$/i, 'special-tags#$1'], + [/template-syntax-(tags|attributes-and-props|text-expressions|comments)$/i, 'basic-markup#$1'], + // !!!! This one should stay at the bottom of `template-syntax`, or it may end up hijacking logic blocks and special tags + [/template-syntax-(.+)/i, 'special-elements#$1'], + + // run-time + [/run-time-(svelte-(?:store|motion|transition|animate))-?(.*)/i, '$1#$2'], + [/run-time-(client-side-component-api)-?(.*)/i, '$1#$2'], + [/run-time-(svelte-easing|server-side-component-api|custom-element-api|svelte-register)$/i, '$1'], + // Catch all, should be at the end or will include store, motion, transition and other modules starting with svelte + [/run-time-(svelte)(?:-(.+))?/i, '$1#$2'], + + // Compile time + [/compile-time-svelte-?(.*)/i, 'svelte-compiler#$1'], + + // Accessibility warnings + [/(accessibility-warnings)-?(.+)/i, '$1#$2'] +]; + +function get_url_to_redirect_to() { + const hash = location.hash.slice(1); + if (!hash) return '/docs/introduction'; + + for (const [regex, replacement] of pages_regex_map) { + if (regex.test(hash)) { + return `/docs/${ + hash + .replace(regex, replacement) + .replace(/#$/, '') // Replace trailing # at the end + .replace('#--', '#') // have to do the -- replacement because of `--style-props` in old being `style-props` in new + }`; + } + } + + // ID doesn't match anything, take the user to intro page only + return '/docs/introduction'; +} + +location.href = new URL(get_url_to_redirect_to(), location.origin).href;