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

[fix] invalid amp-install-serviceworker #3075

Merged
merged 6 commits into from Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-parents-love.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix invalid amp-install-serviceworker
38 changes: 19 additions & 19 deletions packages/kit/src/runtime/server/page/render.js
Expand Up @@ -153,10 +153,8 @@ export async function render_response({
</script>`;
}

if (options.service_worker) {
init += options.amp
? `<amp-install-serviceworker src="${options.service_worker}" layout="nodisplay"></amp-install-serviceworker>`
: `<script>
if (options.service_worker && !options.amp) {
init += `<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('${options.service_worker}');
}
Expand All @@ -172,21 +170,23 @@ export async function render_response({
init
].join('\n\n\t\t');

const body = options.amp
? rendered.html
: `${rendered.html}

${serialized_data
.map(({ url, body, json }) => {
let attributes = `type="application/json" data-type="svelte-data" data-url=${escape_html_attr(
url
)}`;
if (body) attributes += ` data-body="${hash(body)}"`;

return `<script ${attributes}>${json}</script>`;
})
.join('\n\n\t')}
`;
let body = rendered.html;
if (options.amp) {
if (options.service_worker) {
body += `<amp-install-serviceworker src="${options.service_worker}" layout="nodisplay"></amp-install-serviceworker>`;
}
} else {
body += serialized_data
.map(({ url, body, json }) => {
let attributes = `type="application/json" data-type="svelte-data" data-url=${escape_html_attr(
url
)}`;
if (body) attributes += ` data-body="${hash(body)}"`;

return `<script ${attributes}>${json}</script>`;
})
.join('\n\n\t');
}

/** @type {import('types/helper').ResponseHeaders} */
const headers = {
Expand Down