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

remove built-in AMP support #4710

Merged
merged 31 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1c86c83
remove built-in AMP support
Rich-Harris Apr 23, 2022
f4774d2
remove config.kit.amp from docs
Rich-Harris Apr 23, 2022
681923d
Merge branch 'master' into gh-2603
Rich-Harris Apr 25, 2022
445482e
merge master -> gh-2603
Rich-Harris May 18, 2022
4b31fdf
get tests passing
Rich-Harris May 18, 2022
c767817
throw error on encountering stylesheet links
Rich-Harris May 18, 2022
4e4bca7
strip disabled stylesheets, throw on others
Rich-Harris May 18, 2022
48dd270
remove http-equiv tags
Rich-Harris May 18, 2022
e1c6ede
docs
Rich-Harris May 18, 2022
3612fe5
get docs to build
Rich-Harris May 18, 2022
448e20e
changeset
Rich-Harris May 18, 2022
22a9eab
merge master -> gh-2603
Rich-Harris May 19, 2022
5402ddd
move amp stuff into separate package
Rich-Harris May 19, 2022
7a6a481
Update documentation/docs/16-seo.md
Rich-Harris May 19, 2022
8b1a606
update docs
Rich-Harris May 19, 2022
af281a4
lint
Rich-Harris May 19, 2022
285fa25
get site to build
Rich-Harris May 19, 2022
19fbfb7
disable side-effects-cache
Rich-Harris May 19, 2022
d0ffad5
good riddance to amphtml-validator
Rich-Harris May 19, 2022
e392f2e
goddammit
Rich-Harris May 19, 2022
baa0fe8
goodbye, thanks for nothing
Rich-Harris May 19, 2022
27ccc81
bypass turbo for a second
Rich-Harris May 19, 2022
ad66d26
remove rimraf for a second
Rich-Harris May 19, 2022
d7ee9ab
maybe this will please the windows gods
Rich-Harris May 19, 2022
9093045
once more with feeling
Rich-Harris May 19, 2022
0e74399
fuck you, windows
Rich-Harris May 19, 2022
2c56a3e
Merge branch 'master' into gh-2603
Rich-Harris May 20, 2022
95e0bff
revert rimraf changes
Rich-Harris May 20, 2022
b26abe8
rename kit-amp to amp
Rich-Harris May 20, 2022
e08a874
Update .npmrc
Rich-Harris May 20, 2022
4c96bff
move kit-amp to amp
Rich-Harris May 20, 2022
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
5 changes: 5 additions & 0 deletions .changeset/big-cups-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] remove amp config option in favour of amp.transform helper function
5 changes: 0 additions & 5 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const config = {

kit: {
adapter: undefined,
amp: false,
appDir: '_app',
browser: {
hydrate: true,
Expand Down Expand Up @@ -92,10 +91,6 @@ export default config;

Required when running `svelte-kit build` and determines how the output is converted for different platforms. See [Adapters](/docs/adapters).

### amp

Enable [AMP](/docs/seo#amp) mode.

### appDir

The directory relative to `paths.assets` where the built JS and CSS (and imported assets) are served from. (The filenames therein contain content-based hashes, meaning they can be cached indefinitely). Must not start or end with `/`.
Expand Down
42 changes: 37 additions & 5 deletions documentation/docs/16-seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SvelteKit redirects pathnames with trailing slashes to ones without (or vice ver

### Manual setup

#### <title> and <meta>
#### <title> and <meta>

Every page should have well-written and unique `<title>` and `<meta name="description">` elements inside a [`<svelte:head>`](https://svelte.dev/docs#template-syntax-svelte-head). Guidance on how to write descriptive titles and descriptions, along with other suggestions on making content understandable by search engines, can be found on Google's [Lighthouse SEO audits](https://web.dev/lighthouse-seo/) documentation.

Expand Down Expand Up @@ -82,8 +82,40 @@ export async function get() {

#### AMP

An unfortunate reality of modern web development is that it is sometimes necessary to create an [Accelerated Mobile Pages (AMP)](https://amp.dev/) version of your site. In SvelteKit this can be done by setting the [`amp`](/docs/configuration#amp) config option, which has the following effects:
An unfortunate reality of modern web development is that it is sometimes necessary to create an [Accelerated Mobile Pages (AMP)](https://amp.dev/) version of your site. In SvelteKit this can be done by enforcing the following [configuration](/docs/configuration) options...

- Client-side JavaScript, including the router, is disabled
- Styles are concatenated into `<style amp-custom>`, and the [AMP boilerplate](https://amp.dev/boilerplate/) is injected
- In development, requests are checked against the [AMP validator](https://validator.ampproject.org/) so you get early warning of any errors
```js
/// file: svelte.config.js
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// the combination of these options
// disables JavaScript
browser: {
hydrate: false,
router: false
},

// since <link rel="stylesheet"> isn't
// allowed, inline all styles
inlineStyleThreshold: Infinity
}
};

export default config;
```

...and transforming the HTML using `transformPage` along with `transform` imported from `@sveltejs/amp`:

```js
import * as amp from '@sveltejs/amp';

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
return resolve(event, {
transformPage: ({ html }) => amp.transform(html)
});
}
```

> It's a good idea to use the `handle` hook to validate the transformed HTML using `amphtml-validator`, but only if you're prerendering pages since it's very slow.
4 changes: 3 additions & 1 deletion packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ function get_node_version() {
const major = parseInt(full.split('.')[0]); // '16.5.0' --> 16

if (major < 16) {
throw new Error(`SvelteKit only supports Node.js version 16 or greater (currently using v${full}). Consult the documentation: https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version`)
throw new Error(
`SvelteKit only supports Node.js version 16 or greater (currently using v${full}). Consult the documentation: https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version`
);
}

return { major, full };
Expand Down
Empty file added packages/amp/.gitignore
Empty file.
1 change: 1 addition & 0 deletions packages/amp/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function transform(html: string): string;
28 changes: 28 additions & 0 deletions packages/amp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://amp.dev/documentation/guides-and-tutorials/learn/spec/amp-boilerplate/
const boilerplate = `
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<link rel="preload" as="script" href="https://cdn.ampproject.org/v0.js">
<script async src="https://cdn.ampproject.org/v0.js"></script>
`;

/** @param {string} html */
export function transform(html) {
return html
.replace(/<style([^]+?)<\/style>/, (match, $1) => `<style amp-custom${$1}</style>`)
.replace(/<script[^]+?<\/script>/g, '')
.replace(/<link[^>]+>/g, (match) => {
if (/rel=('|")?stylesheet\1/.test(match)) {
if (/ disabled /.test(match)) return '';
throw new Error(
'An AMP document cannot contain <link rel="stylesheet"> — ensure that inlineStyleThreshold is set to Infinity, and remove links from your page template and <svelte:head> elements'
);
}

return match;
})
.replace(/<meta[^>]+>/g, (match) => {
if (match.includes('http-equiv')) return '';
return match;
})
.replace('</head>', boilerplate + '</head>');
}
28 changes: 28 additions & 0 deletions packages/amp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@sveltejs/amp",
"version": "1.0.0-next.0",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
"directory": "packages/adapter-auto"
},
"license": "MIT",
"homepage": "https://kit.svelte.dev",
"type": "module",
"exports": {
".": {
"import": "./index.js"
},
"./package.json": "./package.json"
},
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js"
],
"scripts": {
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
"format": "npm run check-format -- --write",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
}
}
2 changes: 0 additions & 2 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
"devDependencies": {
"@playwright/test": "^1.21.0",
"@rollup/plugin-replace": "^4.0.0",
"@types/amphtml-validator": "^1.0.1",
"@types/connect": "^3.4.35",
"@types/cookie": "^0.5.0",
"@types/marked": "^4.0.1",
"@types/mime": "^2.0.3",
"@types/node": "^16.11.11",
"@types/sade": "^1.7.3",
"@types/set-cookie-parser": "^2.4.2",
"amphtml-validator": "^1.0.35",
"cookie": "^0.5.0",
"cross-env": "^7.0.3",
"devalue": "^2.0.1",
Expand Down
9 changes: 1 addition & 8 deletions packages/kit/src/core/build/build_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export async function build_client({
process.env.VITE_SVELTEKIT_APP_VERSION_FILE = `${config.kit.appDir}/version.json`;
process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = `${config.kit.version.pollInterval}`;

process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : '';

const client_out_dir = `${output_dir}/client/${config.kit.appDir}`;

/** @type {Record<string, string>} */
Expand Down Expand Up @@ -79,12 +77,7 @@ export async function build_client({
plugins: [
svelte({
...config,
// In AMP mode, we know that there are no conditional component imports. In that case, we
// don't need to include CSS for components that are imported but unused, so we can just
// include rendered CSS.
// This would also apply if hydrate and router are both false, but we don't know if one
// has been enabled at the page level, so we don't do anything there.
emitCss: !config.kit.amp,
emitCss: true,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export function override(settings) {
export class Server {
constructor(manifest) {
this.options = {
amp: ${config.kit.amp},
csp: ${s(config.kit.csp)},
dev: false,
floc: ${config.kit.floc},
Expand Down Expand Up @@ -260,7 +259,7 @@ export async function build_server(

client.assets.forEach((asset) => {
if (asset.fileName.endsWith('.css')) {
if (config.kit.amp || asset.source.length < config.kit.inlineStyleThreshold) {
if (asset.source.length < config.kit.inlineStyleThreshold) {
const index = stylesheet_lookup.size;
const file = `${output_dir}/server/stylesheets/${index}.js`;

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const get_defaults = (prefix = '') => ({
extensions: ['.svelte'],
kit: {
adapter: null,
amp: false,
amp: undefined,
appDir: '_app',
browser: {
hydrate: true,
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const options = object(
return input;
}),

amp: boolean(false),
// TODO: remove this for the 1.0 release
amp: error(
(keypath) =>
`${keypath} has been removed. See https://kit.svelte.dev/docs/seo#amp for details on how to support AMP`
),

appDir: validate('_app', (input, keypath) => {
assert_string(input, keypath);
Expand Down
54 changes: 0 additions & 54 deletions packages/kit/src/core/dev/amp_hook.js

This file was deleted.

7 changes: 1 addition & 6 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ export async function dev({ cwd, port, host, https, config }) {
plugins: [
svelte({
...config,
// In AMP mode, we know that there are no conditional component imports. In that case, we
// don't need to include CSS for components that are imported but unused, so we can just
// include rendered CSS.
// This would also apply if hydrate and router are both false, but we don't know if one
// has been enabled at the page level, so we don't do anything there.
emitCss: !config.kit.amp,
emitCss: true,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
Expand Down
12 changes: 1 addition & 11 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { SVELTE_KIT_ASSETS } from '../constants.js';
import { get_mime_lookup, get_runtime_path, resolve_entry } from '../utils.js';
import { coalesce_to_error } from '../../utils/error.js';
import { load_template } from '../config/index.js';
import { sequence } from '../../hooks.js';
import { posixify } from '../../utils/filesystem.js';
import { parse_route_id } from '../../utils/routing.js';

Expand All @@ -26,14 +25,6 @@ const style_pattern = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/;
export async function create_plugin(config, cwd) {
const runtime = get_runtime_path(config);

/** @type {import('types').Handle} */
let amp;

if (config.kit.amp) {
process.env.VITE_SVELTEKIT_AMP = 'true';
amp = (await import('./amp_hook.js')).handle;
}

process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = '0';

/** @type {import('types').Respond} */
Expand Down Expand Up @@ -220,7 +211,7 @@ export async function create_plugin(config, cwd) {
/** @type {import('types').Hooks} */
const hooks = {
getSession: user_hooks.getSession || (() => ({})),
handle: amp ? sequence(amp, handle) : handle,
handle,
handleError:
user_hooks.handleError ||
(({ /** @type {Error & { frame?: string }} */ error }) => {
Expand Down Expand Up @@ -282,7 +273,6 @@ export async function create_plugin(config, cwd) {
const rendered = await respond(
request,
{
amp: config.kit.amp,
csp: config.kit.csp,
dev: true,
floc: config.kit.floc,
Expand Down
4 changes: 0 additions & 4 deletions packages/kit/src/runtime/app/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ export const dev = !!import.meta.env.DEV;
* @type {import('$app/env').mode}
*/
export const mode = import.meta.env.MODE;
/**
* @type {import('$app/env').amp}
*/
export const amp = !!import.meta.env.VITE_SVELTEKIT_AMP;

export { prerendering } from '../env.js';
Loading