diff --git a/docs/canary/concepts/plugins.md b/docs/canary/concepts/plugins.md index 4d6ddce0..5c663748 100644 --- a/docs/canary/concepts/plugins.md +++ b/docs/canary/concepts/plugins.md @@ -80,12 +80,15 @@ client. The `render` hook needs to synchronously return a [`PluginRenderResult`](https://deno.land/x/fresh/server.ts?s=PluginRenderResult) object. Additional CSS and JS modules can be added to be injected into the page -by adding them to `styles` and `scripts` arrays in this object. +by adding them to `styles`, `links` and `scripts` arrays in this object. `styles` are injected into the `
` of the page as inline CSS. Each entry can define the CSS text to inject, as well as an optional `id` for the style tag, and an optional `media` attribute for the style tag. +`links` are injected into the `` of the page as `` tags. A link tag +is created for each entry, with attributes from the entry's properties. + `scripts` define JavaScript/TypeScript modules to be injected into the page. The possibly loaded modules need to be defined up front in the `Plugin#entrypoints` property. Each defined module must be a JavaScript/TypeScript module that has a diff --git a/src/server/rendering/fresh_tags.tsx b/src/server/rendering/fresh_tags.tsx index fdb95329..d57cfe1d 100644 --- a/src/server/rendering/fresh_tags.tsx +++ b/src/server/rendering/fresh_tags.tsx @@ -2,7 +2,12 @@ import { bundleAssetUrl } from "../constants.ts"; import { RenderState } from "./state.ts"; import { htmlEscapeJsonString } from "../htmlescape.ts"; import { serialize } from "../serializer.ts"; -import { Plugin, PluginRenderResult, PluginRenderStyleTag } from "../types.ts"; +import { + Plugin, + PluginRenderLink, + PluginRenderResult, + PluginRenderStyleTag, +} from "../types.ts"; import { ContentSecurityPolicy, nonce } from "../../runtime/csp.ts"; import { h } from "preact"; @@ -52,6 +57,7 @@ export function renderFreshTags( [], ]; const styleTags: PluginRenderStyleTag[] = []; + const linkTags: PluginRenderLink[] = []; const pluginScripts: [string, string, number][] = []; for (const [plugin, res] of opts.pluginRenderResults) { @@ -60,6 +66,7 @@ export function renderFreshTags( pluginScripts.push([plugin.name, hydrate.entrypoint, i]); } styleTags.splice(styleTags.length, 0, ...res.styles ?? []); + linkTags.splice(linkTags.length, 0, ...res.links ?? []); } // The inline script that will hydrate the page. @@ -180,5 +187,10 @@ export function renderFreshTags( renderState.headVNodes.splice(0, 0, node); } + for (const link of linkTags) { + const node = h("link", link); + renderState.headVNodes.splice(0, 0, node); + } + return { bodyHtml: opts.bodyHtml, preloadSet, moduleScripts }; } diff --git a/src/server/types.ts b/src/server/types.ts index b353dcc4..08618b94 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -599,6 +599,8 @@ export interface PluginRenderResult { styles?: PluginRenderStyleTag[]; /** JS scripts to ship to the client. */ scripts?: PluginRenderScripts[]; + /** Link tags for the page */ + links?: PluginRenderLink[]; } export interface PluginRenderStyleTag { @@ -607,6 +609,17 @@ export interface PluginRenderStyleTag { id?: string; } +export type PluginRenderLink = { + crossOrigin?: string; + href?: string; + hreflang?: string; + media?: string; + referrerPolicy?: string; + rel?: string; + title?: string; + type?: string; +}; + export interface PluginRenderScripts { /** The "key" of the entrypoint (as specified in `Plugin.entrypoints`) for the * script that should be loaded. The script must be an ES module that exports diff --git a/tests/fixture_plugin/fresh.config.ts b/tests/fixture_plugin/fresh.config.ts index b1d9701e..f50e9108 100644 --- a/tests/fixture_plugin/fresh.config.ts +++ b/tests/fixture_plugin/fresh.config.ts @@ -2,6 +2,7 @@ import { FreshConfig } from "$fresh/server.ts"; import cssInjectPlugin from "./utils/css-inject-plugin.ts"; import jsInjectPlugin from "./utils/js-inject-plugin.ts"; import cssInjectPluginAsync from "./utils/css-inject-plugin-async.ts"; +import linkInjectPlugin from "./utils/link-inject-plugin.ts"; import routePlugin from "./utils/route-plugin.ts"; import secondMiddlewarePlugin from "$fresh/tests/fixture_plugin/utils/second-middleware-plugin.ts"; @@ -10,6 +11,7 @@ export default { cssInjectPlugin, jsInjectPlugin, cssInjectPluginAsync, + linkInjectPlugin, routePlugin({ title: "Title Set From Plugin Config" }), secondMiddlewarePlugin(), ], diff --git a/tests/fixture_plugin/utils/link-inject-plugin.ts b/tests/fixture_plugin/utils/link-inject-plugin.ts new file mode 100644 index 00000000..aecd1646 --- /dev/null +++ b/tests/fixture_plugin/utils/link-inject-plugin.ts @@ -0,0 +1,15 @@ +import { Plugin } from "$fresh/server.ts"; + +export default { + name: "link-inject", + render(ctx) { + ctx.render(); + return { + links: [{ rel: "stylesheet", href: "styles.css" }, { + rel: "stylesheet", + href: "print.css", + media: "print", + }], + }; + }, +} as Plugin; diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index 0ba3fe47..55abfba1 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -41,6 +41,11 @@ Deno.test("/static page prerender", async () => { body, '', ); + assertStringIncludes(body, ''); + assertStringIncludes( + body, + '', + ); }); Deno.test("/with-island prerender", async () => {