@@ -116,18 +116,53 @@ export const csr = false;
116116...and transforming the HTML using ` transformPageChunk ` along with ` transform ` imported from ` @sveltejs/amp ` :
117117
118118``` js
119+ // / file: src/hooks.server.js
119120import * as amp from ' @sveltejs/amp' ;
120121
121122/** @type {import('@sveltejs/kit').Handle} */
122123export async function handle ({ event , resolve }) {
123124 let buffer = ' ' ;
124- return resolve (event , {
125+ return await resolve (event , {
125126 transformPageChunk : ({ html, done }) => {
126127 buffer += html;
127- if (done) return amp .transform (html );
128+ if (done) return amp .transform (buffer );
128129 }
129130 });
130131}
131132```
132133
134+ 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 ) :
135+
136+ ``` js
137+ // / file: src/hooks.server.js
138+ import * as amp from ' @sveltejs/amp' ;
139+ import dropcss from ' dropcss' ;
140+
141+ /** @type {import('@sveltejs/kit').Handle} */
142+ export async function handle ({ event , resolve }) {
143+ let buffer = ' ' ;
144+
145+ return await resolve (event , {
146+ transformPageChunk : ({ html, done }) => {
147+ buffer += html;
148+
149+ if (done) {
150+ let css = ' ' ;
151+ const markup = amp
152+ .transform (buffer)
153+ .replace (' ⚡' , ' amp' )
154+ .replace (/ <style amp-custom([^ >] *? )>([^ ] +? )<\/ style>/ , (match , attributes , contents ) => {
155+ css = contents;
156+ return ` <style amp-custom${ attributes} ></style>` ;
157+ });
158+
159+ css = dropcss ({ css, html: markup }).css ;
160+ return markup .replace (' </style>' , ` ${ css} </style>` );
161+ }
162+ }
163+ });
164+ }
165+
166+ ```
167+
133168> 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.
0 commit comments