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

remove built-in AMP support #4710

merged 31 commits into from
May 20, 2022

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Apr 23, 2022

This removes the amp: true config option in favour of a more appropriate solution (we despise AMP and want to hasten its demise, but still need to provide some way for people to deal with it).

It adds a new @sveltejs/kit/amp module which exports a transform function, which:

  • removes all <script> tags
  • ensures there are no <link rel="stylesheet"> elements
  • adds an amp-custom attribute to <style>
  • removes <meta http-equiv> elements
  • adds the AMP boilerplate
  • adds <amp-install-serviceworker> if appropriate
  • CSP CSP support for AMP pages #3557

Some of these are TODO and I haven't quite figured out how to solve them. Also TODO:

  • docs

Using it is relatively straightforward — set inlineStyleThreshold to Infinity (external stylesheets are disallowed), and add the following to src/hooks.js:

import * as amp from '@sveltejs/kit/amp';

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

One thing we lose is the ability to exclude CSS from components that are imported but not rendered for a given page. Given AMP's wholly arbitrary CSS size limit, that can be a problem. This can be solved (at least for prerendered pages, where it doesn't matter how expensive this sort of processing is) with something like https://github.com/purifycss/purifycss:

import purify from 'purify-css';
import * as amp from '@sveltejs/kit/amp';

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

      // remove unused CSS
      let css = '';
      const markup = html.replace(
        /<style([^>]*?)>([^]+?)<\/style>/,
        (match, attributes, contents) => {
          css = contents;
          return `<style${attributes}></style>`;
        }
      );

      css = purify(markup, css);
      return markup.replace('</style>', `${css}</style>`);
    }
  });
}

It's not quite as turnkey as the current solution, but it's fine.

Another thing we lose is validation. This, too, can easily be solved in userland by using amphtml-validator directly.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Apr 23, 2022

🦋 Changeset detected

Latest commit: 4c96bff

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris Rich-Harris marked this pull request as draft April 23, 2022 13:01
@Rich-Harris
Copy link
Member Author

cc @john-michael-murphy as I know this'll probably affect you lot

@john-michael-murphy
Copy link

Migration looks doable. Thank you for the heads up. AMP is such a thorn!

@Rich-Harris
Copy link
Member Author

There's no good way to do automatic service worker installation (the transformer can't read the config, or paths.base etc), so i'm going to leave that to users, who can easily inject <amp-install-serviceworker> themselves

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

having kit in the name kit-amp seems a bit redundant. maybe just amp?

.npmrc Outdated Show resolved Hide resolved
@Rich-Harris
Copy link
Member Author

having kit in the name kit-amp seems a bit redundant. maybe just amp?

originally i was thinking that we might end up having kit-specific stuff in there, but i'm inclined to do this just because my muscles are so used to cd pac[tab]/k[tab]/t[tab]/a[tab]/b[tab taking me to packages/kit/test/apps/basics and it doesn't any more, and it will make me resent AMP even more than i currently do

Co-authored-by: Ben McCann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants