@unhead/svelte 2.1.8 → 3.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.mjs CHANGED
@@ -1,15 +1,13 @@
1
1
  import { tick } from 'svelte';
2
- import { createHead as createHead$1, createDebouncedFn, renderDOMHead } from 'unhead/client';
2
+ import { createDomRenderer, createDebouncedFn, createHead as createHead$1 } from 'unhead/client';
3
3
  export { renderDOMHead } from 'unhead/client';
4
- export { U as UnheadContextKey } from './shared/svelte.Cczd8v9v.mjs';
4
+ export { U as UnheadContextKey } from './shared/svelte.89y3fBMp.mjs';
5
5
 
6
6
  function createHead(options = {}) {
7
- const head = createHead$1({
8
- domOptions: {
9
- render: createDebouncedFn(() => renderDOMHead(head), (fn) => tick().then(fn))
10
- },
11
- ...options
12
- });
7
+ const domRenderer = createDomRenderer();
8
+ let head;
9
+ const debouncedRenderer = createDebouncedFn(() => domRenderer(head), (fn) => tick().then(fn));
10
+ head = createHead$1({ render: debouncedRenderer, ...options });
13
11
  return head;
14
12
  }
15
13
 
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { onMount, onDestroy, getContext } from 'svelte';
2
2
  import { useHead as useHead$1, useHeadSafe as useHeadSafe$1, useScript as useScript$1, useSeoMeta as useSeoMeta$1 } from 'unhead';
3
- import { U as UnheadContextKey } from './shared/svelte.Cczd8v9v.mjs';
3
+ import { U as UnheadContextKey } from './shared/svelte.89y3fBMp.mjs';
4
4
 
5
5
  const autoImports = {
6
6
  "@unhead/svelte": [
@@ -19,9 +19,11 @@ function useUnhead() {
19
19
  return instance;
20
20
  }
21
21
  function withSideEffects(instance) {
22
- onDestroy(() => {
23
- instance.dispose();
24
- });
22
+ if (typeof window !== "undefined") {
23
+ onDestroy(() => {
24
+ instance.dispose();
25
+ });
26
+ }
25
27
  return instance;
26
28
  }
27
29
  function useHead(input = {}, options = {}) {
package/dist/server.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- export { createHead, extractUnheadInputFromHtml, renderSSRHead, transformHtmlTemplate } from 'unhead/server';
1
+ export { createHead, renderSSRHead, transformHtmlTemplate } from 'unhead/server';
2
2
  export { CreateServerHeadOptions, SSRHeadPayload, Unhead } from 'unhead/types';
3
3
 
4
4
  declare const UnheadContextKey: unique symbol;
package/dist/server.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createHead, extractUnheadInputFromHtml, renderSSRHead, transformHtmlTemplate } from 'unhead/server';
1
+ export { createHead, renderSSRHead, transformHtmlTemplate } from 'unhead/server';
2
2
  export { CreateServerHeadOptions, SSRHeadPayload, Unhead } from 'unhead/types';
3
3
 
4
4
  declare const UnheadContextKey: unique symbol;
package/dist/server.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { U as UnheadContextKey } from './shared/svelte.Cczd8v9v.mjs';
2
- export { createHead, extractUnheadInputFromHtml, renderSSRHead, transformHtmlTemplate } from 'unhead/server';
1
+ export { U as UnheadContextKey } from './shared/svelte.89y3fBMp.mjs';
2
+ export { createHead, renderSSRHead, transformHtmlTemplate } from 'unhead/server';
@@ -0,0 +1,3 @@
1
+ const UnheadContextKey = Symbol.for("unhead");
2
+
3
+ export { UnheadContextKey as U };
@@ -0,0 +1,11 @@
1
+ export { UnheadContextKey } from '../server.mjs';
2
+ export { CreateStreamableClientHeadOptions, UnheadStreamQueue, createStreamableHead } from 'unhead/stream/client';
3
+ import 'unhead/server';
4
+ import 'unhead/types';
5
+
6
+ /**
7
+ * Client-side HeadStream - returns empty string (script already executed during SSR streaming)
8
+ */
9
+ declare function HeadStream(): string;
10
+
11
+ export { HeadStream };
@@ -0,0 +1,11 @@
1
+ export { UnheadContextKey } from '../server.js';
2
+ export { CreateStreamableClientHeadOptions, UnheadStreamQueue, createStreamableHead } from 'unhead/stream/client';
3
+ import 'unhead/server';
4
+ import 'unhead/types';
5
+
6
+ /**
7
+ * Client-side HeadStream - returns empty string (script already executed during SSR streaming)
8
+ */
9
+ declare function HeadStream(): string;
10
+
11
+ export { HeadStream };
@@ -0,0 +1,8 @@
1
+ export { U as UnheadContextKey } from '../shared/svelte.89y3fBMp.mjs';
2
+ export { createStreamableHead } from 'unhead/stream/client';
3
+
4
+ function HeadStream() {
5
+ return "";
6
+ }
7
+
8
+ export { HeadStream };
@@ -0,0 +1,37 @@
1
+ import { WebStreamableHeadContext } from 'unhead/stream/server';
2
+ export { CreateStreamableServerHeadOptions, StreamingTemplateParts, WebStreamableHeadContext, prepareStreamingTemplate, renderSSRHeadShell, renderSSRHeadSuspenseChunk, wrapStream } from 'unhead/stream/server';
3
+ import { CreateStreamableServerHeadOptions } from 'unhead/types';
4
+ export { Unhead } from 'unhead/types';
5
+ export { UnheadContextKey } from '../server.mjs';
6
+ import 'unhead/server';
7
+
8
+ /**
9
+ * Streaming script function for Svelte.
10
+ * Returns HTML string to be rendered with {@html}.
11
+ *
12
+ * Note: Svelte SSR is synchronous, so all head entries are captured in the shell.
13
+ * This function exists for API parity with React/Solid but returns empty for Svelte
14
+ * since there are no streaming head updates to emit.
15
+ */
16
+ declare function HeadStream(): string;
17
+ /**
18
+ * Svelte streaming context returned by createStreamableHead.
19
+ * Type alias for WebStreamableHeadContext from core.
20
+ */
21
+ type SvelteStreamableHeadContext = WebStreamableHeadContext;
22
+ /**
23
+ * Creates a head instance configured for Svelte streaming SSR.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const { head, wrapStream } = createStreamableHead()
28
+ * setContext(UnheadContextKey, head)
29
+ *
30
+ * const stream = render(App)
31
+ * return wrapStream(stream, template)
32
+ * ```
33
+ */
34
+ declare function createStreamableHead(options?: CreateStreamableServerHeadOptions): SvelteStreamableHeadContext;
35
+
36
+ export { HeadStream, createStreamableHead };
37
+ export type { SvelteStreamableHeadContext };
@@ -0,0 +1,37 @@
1
+ import { WebStreamableHeadContext } from 'unhead/stream/server';
2
+ export { CreateStreamableServerHeadOptions, StreamingTemplateParts, WebStreamableHeadContext, prepareStreamingTemplate, renderSSRHeadShell, renderSSRHeadSuspenseChunk, wrapStream } from 'unhead/stream/server';
3
+ import { CreateStreamableServerHeadOptions } from 'unhead/types';
4
+ export { Unhead } from 'unhead/types';
5
+ export { UnheadContextKey } from '../server.js';
6
+ import 'unhead/server';
7
+
8
+ /**
9
+ * Streaming script function for Svelte.
10
+ * Returns HTML string to be rendered with {@html}.
11
+ *
12
+ * Note: Svelte SSR is synchronous, so all head entries are captured in the shell.
13
+ * This function exists for API parity with React/Solid but returns empty for Svelte
14
+ * since there are no streaming head updates to emit.
15
+ */
16
+ declare function HeadStream(): string;
17
+ /**
18
+ * Svelte streaming context returned by createStreamableHead.
19
+ * Type alias for WebStreamableHeadContext from core.
20
+ */
21
+ type SvelteStreamableHeadContext = WebStreamableHeadContext;
22
+ /**
23
+ * Creates a head instance configured for Svelte streaming SSR.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const { head, wrapStream } = createStreamableHead()
28
+ * setContext(UnheadContextKey, head)
29
+ *
30
+ * const stream = render(App)
31
+ * return wrapStream(stream, template)
32
+ * ```
33
+ */
34
+ declare function createStreamableHead(options?: CreateStreamableServerHeadOptions): SvelteStreamableHeadContext;
35
+
36
+ export { HeadStream, createStreamableHead };
37
+ export type { SvelteStreamableHeadContext };
@@ -0,0 +1,23 @@
1
+ import { createStreamableHead as createStreamableHead$1, wrapStream } from 'unhead/stream/server';
2
+ export { prepareStreamingTemplate, renderSSRHeadShell, renderSSRHeadSuspenseChunk, wrapStream } from 'unhead/stream/server';
3
+ export { U as UnheadContextKey } from '../shared/svelte.89y3fBMp.mjs';
4
+
5
+ function HeadStream() {
6
+ return "";
7
+ }
8
+ function createStreamableHead(options = {}) {
9
+ const { head } = createStreamableHead$1(options);
10
+ let shellRendered = false;
11
+ head._shellRendered = () => shellRendered;
12
+ return {
13
+ head,
14
+ wrapStream: (stream, template) => {
15
+ const preRenderedState = head.render();
16
+ head.entries.clear();
17
+ shellRendered = true;
18
+ return wrapStream(head, stream, template, preRenderedState);
19
+ }
20
+ };
21
+ }
22
+
23
+ export { HeadStream, createStreamableHead };
@@ -0,0 +1,27 @@
1
+ import * as vite from 'vite';
2
+ import { StreamingPluginOptions } from 'unhead/stream/vite';
3
+
4
+ /**
5
+ * Vite plugin for Svelte streaming SSR support.
6
+ * Automatically injects HeadStream into Svelte components.
7
+ *
8
+ * @returns Vite plugin configuration object with:
9
+ * - `name`: Plugin identifier
10
+ * - `enforce`: Plugin execution order ('pre')
11
+ * - `transform`: Transform hook for processing .svelte files
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // vite.config.ts
16
+ * import { unheadSveltePlugin } from '@unhead/svelte/stream/vite'
17
+ *
18
+ * export default {
19
+ * plugins: [
20
+ * unheadSveltePlugin()
21
+ * ]
22
+ * }
23
+ * ```
24
+ */
25
+ declare function unheadSveltePlugin(options?: Pick<StreamingPluginOptions, 'mode'>): vite.Plugin<any>;
26
+
27
+ export { unheadSveltePlugin as default, unheadSveltePlugin };
@@ -0,0 +1,27 @@
1
+ import * as vite from 'vite';
2
+ import { StreamingPluginOptions } from 'unhead/stream/vite';
3
+
4
+ /**
5
+ * Vite plugin for Svelte streaming SSR support.
6
+ * Automatically injects HeadStream into Svelte components.
7
+ *
8
+ * @returns Vite plugin configuration object with:
9
+ * - `name`: Plugin identifier
10
+ * - `enforce`: Plugin execution order ('pre')
11
+ * - `transform`: Transform hook for processing .svelte files
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // vite.config.ts
16
+ * import { unheadSveltePlugin } from '@unhead/svelte/stream/vite'
17
+ *
18
+ * export default {
19
+ * plugins: [
20
+ * unheadSveltePlugin()
21
+ * ]
22
+ * }
23
+ * ```
24
+ */
25
+ declare function unheadSveltePlugin(options?: Pick<StreamingPluginOptions, 'mode'>): vite.Plugin<any>;
26
+
27
+ export { unheadSveltePlugin as default, unheadSveltePlugin };
@@ -0,0 +1,66 @@
1
+ import MagicString from 'magic-string';
2
+ import { parseAndWalk } from 'oxc-walker';
3
+ import { createStreamingPlugin } from 'unhead/stream/vite';
4
+
5
+ function transform(code, id, isSSR, s) {
6
+ if (!code.includes("useHead") && !code.includes("useSeoMeta") && !code.includes("useHeadSafe"))
7
+ return false;
8
+ const scriptCloseMatch = code.match(/<\/script>/);
9
+ if (!scriptCloseMatch)
10
+ return false;
11
+ const templateStart = scriptCloseMatch.index + scriptCloseMatch[0].length;
12
+ s.appendRight(templateStart, "\n{@html HeadStream()}");
13
+ const importPath = `@unhead/svelte/stream/${isSSR ? "server" : "client"}`;
14
+ const scriptMatch = code.match(/<script[^>]*>/i);
15
+ if (!scriptMatch)
16
+ return true;
17
+ const scriptEnd = scriptMatch.index + scriptMatch[0].length;
18
+ const scriptCloseIndex = code.indexOf("<\/script>", scriptEnd);
19
+ if (scriptCloseIndex === -1)
20
+ return true;
21
+ const scriptContent = code.slice(scriptEnd, scriptCloseIndex);
22
+ let existingImport = null;
23
+ parseAndWalk(scriptContent, id, {
24
+ parseOptions: { lang: "ts" },
25
+ enter(node) {
26
+ if (node.type === "ImportDeclaration" && node.source.value === importPath) {
27
+ existingImport = {
28
+ start: scriptEnd + node.start,
29
+ end: scriptEnd + node.end,
30
+ specifiers: node.specifiers?.map((spec) => spec.local?.name).filter(Boolean) || []
31
+ };
32
+ this.skip();
33
+ }
34
+ }
35
+ });
36
+ const foundImport = existingImport;
37
+ if (foundImport) {
38
+ if (!foundImport.specifiers.includes("HeadStream")) {
39
+ const inner = foundImport.specifiers.join(", ");
40
+ const newImports = inner ? `${inner}, HeadStream` : "HeadStream";
41
+ s.overwrite(foundImport.start, foundImport.end, `import { ${newImports} } from '${importPath}'`);
42
+ }
43
+ } else {
44
+ s.appendRight(scriptEnd, `
45
+ import { HeadStream } from '${importPath}'`);
46
+ }
47
+ return true;
48
+ }
49
+ function unheadSveltePlugin(options) {
50
+ return createStreamingPlugin({
51
+ framework: "@unhead/svelte",
52
+ filter: /\.svelte$/,
53
+ mode: options?.mode,
54
+ transform(code, id, opts) {
55
+ const s = new MagicString(code);
56
+ if (!transform(code, id, opts?.ssr ?? false, s))
57
+ return null;
58
+ return {
59
+ code: s.toString(),
60
+ map: s.generateMap({ includeContent: true, source: id })
61
+ };
62
+ }
63
+ });
64
+ }
65
+
66
+ export { unheadSveltePlugin as default, unheadSveltePlugin };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/svelte",
3
3
  "type": "module",
4
- "version": "2.1.8",
4
+ "version": "3.0.0-beta.10",
5
5
  "description": "Full-stack <head> manager built for Svelte.",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -29,6 +29,14 @@
29
29
  "types": "./dist/client.d.ts",
30
30
  "default": "./dist/client.mjs"
31
31
  },
32
+ "./stream/server": {
33
+ "types": "./dist/stream/server.d.ts",
34
+ "default": "./dist/stream/server.mjs"
35
+ },
36
+ "./stream/client": {
37
+ "types": "./dist/stream/client.d.ts",
38
+ "default": "./dist/stream/client.mjs"
39
+ },
32
40
  "./utils": {
33
41
  "types": "./dist/utils.d.ts",
34
42
  "default": "./dist/utils.mjs"
@@ -36,6 +44,10 @@
36
44
  "./plugins": {
37
45
  "types": "./dist/plugins.d.ts",
38
46
  "default": "./dist/plugins.mjs"
47
+ },
48
+ "./stream/vite": {
49
+ "types": "./dist/stream/vite.d.ts",
50
+ "default": "./dist/stream/vite.mjs"
39
51
  }
40
52
  },
41
53
  "main": "dist/index.mjs",
@@ -48,6 +60,21 @@
48
60
  ],
49
61
  "client": [
50
62
  "dist/client"
63
+ ],
64
+ "stream/server": [
65
+ "dist/stream/server"
66
+ ],
67
+ "stream/client": [
68
+ "dist/stream/client"
69
+ ],
70
+ "plugins": [
71
+ "dist/plugins"
72
+ ],
73
+ "utils": [
74
+ "dist/utils"
75
+ ],
76
+ "stream/vite": [
77
+ "dist/stream/vite"
51
78
  ]
52
79
  }
53
80
  },
@@ -56,7 +83,13 @@
56
83
  "dist"
57
84
  ],
58
85
  "peerDependencies": {
59
- "svelte": ">=5.38.0"
86
+ "svelte": ">=5.38.0",
87
+ "vite": ">=6"
88
+ },
89
+ "peerDependenciesMeta": {
90
+ "vite": {
91
+ "optional": true
92
+ }
60
93
  },
61
94
  "build": {
62
95
  "external": [
@@ -64,12 +97,16 @@
64
97
  ]
65
98
  },
66
99
  "dependencies": {
67
- "unhead": "2.1.8"
100
+ "magic-string": "^0.30.21",
101
+ "oxc-parser": "^0.106.0",
102
+ "oxc-walker": "^0.7.0",
103
+ "unhead": "3.0.0-beta.10"
68
104
  },
69
105
  "devDependencies": {
70
106
  "@sveltejs/vite-plugin-svelte": "^6.2.4",
71
107
  "@testing-library/svelte": "^5.3.1",
72
- "svelte": "^5.46.1"
108
+ "svelte": "^5.46.1",
109
+ "vite": "^7.2.2"
73
110
  },
74
111
  "scripts": {
75
112
  "build": "unbuild .",
@@ -1,3 +0,0 @@
1
- const UnheadContextKey = Symbol("unhead");
2
-
3
- export { UnheadContextKey as U };