Allow plugin render hooks to add link tags to the page (#1963)
Adds an extra optional property to plugin render results to allow plugins to add `<link>` tags to the page.
This commit is contained in:
@@ -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 `<head>` 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 `<head>` of the page as `<link>` 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
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
],
|
||||
|
||||
@@ -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;
|
||||
@@ -41,6 +41,11 @@ Deno.test("/static page prerender", async () => {
|
||||
body,
|
||||
'<style id="def">h1 { text-decoration: underline; }</style>',
|
||||
);
|
||||
assertStringIncludes(body, '<link rel="stylesheet" href="styles.css"/>');
|
||||
assertStringIncludes(
|
||||
body,
|
||||
'<link rel="stylesheet" href="print.css" media="print"/>',
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("/with-island prerender", async () => {
|
||||
|
||||
Reference in New Issue
Block a user