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

site: quicker redirects to appease the SEO deities #9116

Open
wants to merge 6 commits into
base: svelte-4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sites/svelte.dev/src/routes/docs.html/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const prerender = true;
export { GET } from '../docs/+server.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should make docs.html return anything. It returns "500 | not found" right now, which is really weird, but we should just fix that to return 404

Copy link
Contributor Author

@gtm-nayan gtm-nayan Aug 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to get it to output the same prerendered files as it did before, not sure why it's a 500/404 currently because there is a docs.html in the prerendered output. Some manifest thing maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably sveltejs/kit#10565 will fix the 500/404 thing

https://svelte.dev/docs.html doesn't currently return anything, but would with this PR, so this isn't really making it equivalent to what it was before

216 changes: 0 additions & 216 deletions sites/svelte.dev/src/routes/docs/+page.svelte

This file was deleted.

13 changes: 13 additions & 0 deletions sites/svelte.dev/src/routes/docs/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-ignore custom suffix doesn't have types and cba to add them just for this
import js from 'virtual:minified-raw:./redirect.js';

// avoid outputting a file named "docs" that would conflict with prerendered "docs" directory
export const prerender = false;

export function GET() {
return new Response(`<html><head><script>${js}</script></head></html>`, {
headers: {
'content-type': 'text/html'
}
});
}
58 changes: 58 additions & 0 deletions sites/svelte.dev/src/routes/docs/redirect.js
Original file line number Diff line number Diff line change
@@ -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;
62 changes: 45 additions & 17 deletions sites/svelte.dev/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@ import { sveltekit } from '@sveltejs/kit/vite';
import { browserslistToTargets } from 'lightningcss';
import { readFile } from 'node:fs/promises';
import browserslist from 'browserslist';

const plugins = [raw(['.ttf']), sveltekit()];

// Only enable sharp if we're not in a webcontainer env
if (!process.versions.webcontainer) {
plugins.push(
(await import('vite-imagetools')).imagetools({
defaultDirectives: (url) => {
if (url.searchParams.has('big-image')) {
return new URLSearchParams('w=640;1280;2560;3840&format=avif;webp;png&as=picture');
}

return new URLSearchParams();
}
})
);
}
import { transformWithEsbuild } from 'vite';

/**
* @param {string[]} ext
Expand All @@ -36,6 +20,50 @@ function raw(ext) {
};
}

/** @returns {import('vite').Plugin} */
function minified_raw_plugin() {
const prefix = 'virtual:minified-raw:';

return {
name: 'minified-raw-js',
async resolveId(id, importer) {
if (id.startsWith(prefix)) {
const resolved = await this.resolve(id.slice(prefix.length), importer, { skipSelf: true });
return '\0' + prefix + resolved.id;
}
},
async load(id) {
if (id.startsWith('\0' + prefix)) {
const real_id = id.slice(1 + prefix.length);
const original = await readFile(real_id, 'utf-8');
const { code } = await transformWithEsbuild(original, real_id, {
minify: true,
format: 'esm'
});
return `export default ${JSON.stringify(code)}`;
}
}
};
}

/** @type {import('vite').Plugin[]} */
const plugins = [raw(['.ttf']), sveltekit(), minified_raw_plugin()];

// Only enable sharp if we're not in a webcontainer env
if (!process.versions.webcontainer) {
plugins.push(
(await import('vite-imagetools')).imagetools({
defaultDirectives: (url) => {
if (url.searchParams.has('big-image')) {
return new URLSearchParams('w=640;1280;2560;3840&format=avif;webp;png&as=picture');
}

return new URLSearchParams();
}
})
);
}

/** @type {import('vite').UserConfig} */
const config = {
logLevel: 'info',
Expand Down