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

docs: switch to dropcss in AMP example #10546

Merged
merged 10 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
46 changes: 44 additions & 2 deletions documentation/docs/40-best-practices/20-seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,63 @@ export default config;
export const csr = false;
```

...adding `amp` to your `app.html`

```html
<html amp>
...
```

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

```js
/// file: src/hooks.server.js
import * as amp from '@sveltejs/amp';

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
let buffer = '';
return resolve(event, {
return await resolve(event, {
transformPageChunk: ({ html, done }) => {
buffer += html;
if (done) return amp.transform(html);
if (done) return amp.transform(buffer);
}
});
}
```

To prevent shipping any unused CSS as a result of transforming the page to amp, we can use [`dropcss`](https://www.npmjs.com/package/dropcss):

```js
/// file: src/hooks.server.js
ghostdevv marked this conversation as resolved.
Show resolved Hide resolved
import * as amp from '@sveltejs/amp';
import dropcss from 'dropcss';

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
let buffer = '';

return await resolve(event, {
transformPageChunk: ({ html, done }) => {
buffer += html;

if (done) {
let css = '';
const markup = amp
.transform(buffer)
.replace('⚡', 'amp')
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
.replace(/<style amp-custom([^>]*?)>([^]+?)<\/style>/, (match, attributes, contents) => {
css = contents;
return `<style amp-custom${attributes}></style>`;
});

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

```

> 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.
2 changes: 1 addition & 1 deletion packages/kit/test/apps/amp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@sveltejs/amp": "workspace:^",
"@sveltejs/kit": "workspace:^",
"cross-env": "^7.0.3",
"purify-css": "^1.2.5",
"dropcss": "^1.0.16",
"svelte": "^4.0.5",
"svelte-check": "^3.4.4",
"typescript": "^4.9.4",
Expand Down
14 changes: 14 additions & 0 deletions packages/kit/test/apps/amp/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
/// <reference types="@sveltejs/kit" />

declare module 'dropcss' {
interface Options {
html: string;
css: string;

shouldDrop?: (selector: string) => boolean;
didRetain?: (selector: string) => void;

keepText?: boolean;
}

export default function dropcss(options: Options): { css: string };
}
2 changes: 1 addition & 1 deletion packages/kit/test/apps/amp/src/app.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" amp>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
Expand Down
21 changes: 8 additions & 13 deletions packages/kit/test/apps/amp/src/hooks.server.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import purify from 'purify-css';
import * as amp from '@sveltejs/amp';
import dropcss from 'dropcss';

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
let buffer = '';

const response = await resolve(event, {
return await resolve(event, {
transformPageChunk: ({ html, done }) => {
buffer += html;

if (done) {
const html = amp.transform(buffer);

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

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

return response;
}
Loading
Loading