Skip to content

Commit

Permalink
feat: support server-rendering attributes of <svelte:html> blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Nov 27, 2024
1 parent fb146e7 commit f16f4d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-garlics-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: support server-rendering attributes of `<svelte:html>` blocks
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The `src` directory contains the meat of your project. Everything except `src/ro
- `%sveltekit.assets%` — either [`paths.assets`](configuration#paths), if specified, or a relative path to [`paths.base`](configuration#paths)
- `%sveltekit.nonce%` — a [CSP](configuration#csp) nonce for manually included links and scripts, if used
- `%sveltekit.env.[NAME]%` - this will be replaced at render time with the `[NAME]` environment variable, which must begin with the [`publicPrefix`](configuration#env) (usually `PUBLIC_`). It will fallback to `''` if not matched.
- `%svelte.htmlAttributes%` — the attributes collected from `<svelte:html>` blocks
- `error.html` is the page that is rendered when everything else fails. It can contain the following placeholders:
- `%sveltekit.status%` — the HTTP status
- `%sveltekit.error.message%` — the error message
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { set_private_env, set_public_env, set_safe_public_env } from '${runtime_
export const options = {
app_dir: ${s(config.kit.appDir)},
app_template_contains_nonce: ${template.includes('%sveltekit.nonce%')},
app_template_contains_svelte_htmlAttributes: ${template.includes('%svelte.htmlAttributes%')},
csp: ${s(config.kit.csp)},
csrf_check_origin: ${s(config.kit.csrf.checkOrigin)},
embedded: ${config.kit.embedded},
Expand All @@ -47,7 +48,8 @@ export const options = {
root,
service_worker: ${has_service_worker},
templates: {
app: ({ head, body, assets, nonce, env }) => ${s(template)
app: ({ html_attributes, head, body, assets, nonce, env }) => ${s(template)
.replace('%svelte.htmlAttributes%', '" + html_attributes + "')
.replace('%sveltekit.head%', '" + head + "')
.replace('%sveltekit.body%', '" + body + "')
.replace(/%sveltekit\.assets%/g, '" + assets + "')
Expand Down
15 changes: 14 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ export async function render_response({
}
}
} else {
rendered = { head: '', html: '', css: { code: '', map: null } };
rendered = { head: '', html: '', css: { code: '', map: null }, htmlAttributes: '' };
}

if (
!options.app_template_contains_svelte_htmlAttributes &&
// @ts-expect-error only exists in later versions of Svelte 5
rendered.htmlAttributes
) {
console.warn(
'One or more components used `<svelte:html>` to output attributes to the HTML tag but app.html does not contain %svelte.htmlAttributes% to render them. The attributes will be ignored.'
);
}

let head = '';
Expand Down Expand Up @@ -458,6 +468,9 @@ export async function render_response({
head += rendered.head;

const html = options.templates.app({
// @ts-expect-error only exists in later versions of Svelte 5
html_attributes: rendered.htmlAttributes ?? '',

head,
body,
assets,
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export type SSRNodeLoader = () => Promise<SSRNode>;
export interface SSROptions {
app_dir: string;
app_template_contains_nonce: boolean;
app_template_contains_svelte_htmlAttributes: boolean;
csp: ValidatedConfig['kit']['csp'];
csrf_check_origin: boolean;
embedded: boolean;
Expand All @@ -366,6 +367,7 @@ export interface SSROptions {
service_worker: boolean;
templates: {
app(values: {
htmlAttributes: string;
head: string;
body: string;
assets: string;
Expand Down
2 changes: 1 addition & 1 deletion playgrounds/basic/src/app.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html %svelte.htmlAttributes% lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
Expand Down

0 comments on commit f16f4d1

Please sign in to comment.