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
|
The `render` hook needs to synchronously return a
|
||||||
[`PluginRenderResult`](https://deno.land/x/fresh/server.ts?s=PluginRenderResult)
|
[`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
|
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
|
`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
|
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.
|
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
|
`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`
|
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
|
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 { RenderState } from "./state.ts";
|
||||||
import { htmlEscapeJsonString } from "../htmlescape.ts";
|
import { htmlEscapeJsonString } from "../htmlescape.ts";
|
||||||
import { serialize } from "../serializer.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 { ContentSecurityPolicy, nonce } from "../../runtime/csp.ts";
|
||||||
import { h } from "preact";
|
import { h } from "preact";
|
||||||
|
|
||||||
@@ -52,6 +57,7 @@ export function renderFreshTags(
|
|||||||
[],
|
[],
|
||||||
];
|
];
|
||||||
const styleTags: PluginRenderStyleTag[] = [];
|
const styleTags: PluginRenderStyleTag[] = [];
|
||||||
|
const linkTags: PluginRenderLink[] = [];
|
||||||
const pluginScripts: [string, string, number][] = [];
|
const pluginScripts: [string, string, number][] = [];
|
||||||
|
|
||||||
for (const [plugin, res] of opts.pluginRenderResults) {
|
for (const [plugin, res] of opts.pluginRenderResults) {
|
||||||
@@ -60,6 +66,7 @@ export function renderFreshTags(
|
|||||||
pluginScripts.push([plugin.name, hydrate.entrypoint, i]);
|
pluginScripts.push([plugin.name, hydrate.entrypoint, i]);
|
||||||
}
|
}
|
||||||
styleTags.splice(styleTags.length, 0, ...res.styles ?? []);
|
styleTags.splice(styleTags.length, 0, ...res.styles ?? []);
|
||||||
|
linkTags.splice(linkTags.length, 0, ...res.links ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The inline script that will hydrate the page.
|
// The inline script that will hydrate the page.
|
||||||
@@ -180,5 +187,10 @@ export function renderFreshTags(
|
|||||||
renderState.headVNodes.splice(0, 0, node);
|
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 };
|
return { bodyHtml: opts.bodyHtml, preloadSet, moduleScripts };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -599,6 +599,8 @@ export interface PluginRenderResult {
|
|||||||
styles?: PluginRenderStyleTag[];
|
styles?: PluginRenderStyleTag[];
|
||||||
/** JS scripts to ship to the client. */
|
/** JS scripts to ship to the client. */
|
||||||
scripts?: PluginRenderScripts[];
|
scripts?: PluginRenderScripts[];
|
||||||
|
/** Link tags for the page */
|
||||||
|
links?: PluginRenderLink[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PluginRenderStyleTag {
|
export interface PluginRenderStyleTag {
|
||||||
@@ -607,6 +609,17 @@ export interface PluginRenderStyleTag {
|
|||||||
id?: string;
|
id?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PluginRenderLink = {
|
||||||
|
crossOrigin?: string;
|
||||||
|
href?: string;
|
||||||
|
hreflang?: string;
|
||||||
|
media?: string;
|
||||||
|
referrerPolicy?: string;
|
||||||
|
rel?: string;
|
||||||
|
title?: string;
|
||||||
|
type?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export interface PluginRenderScripts {
|
export interface PluginRenderScripts {
|
||||||
/** The "key" of the entrypoint (as specified in `Plugin.entrypoints`) for the
|
/** 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
|
* 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 cssInjectPlugin from "./utils/css-inject-plugin.ts";
|
||||||
import jsInjectPlugin from "./utils/js-inject-plugin.ts";
|
import jsInjectPlugin from "./utils/js-inject-plugin.ts";
|
||||||
import cssInjectPluginAsync from "./utils/css-inject-plugin-async.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 routePlugin from "./utils/route-plugin.ts";
|
||||||
import secondMiddlewarePlugin from "$fresh/tests/fixture_plugin/utils/second-middleware-plugin.ts";
|
import secondMiddlewarePlugin from "$fresh/tests/fixture_plugin/utils/second-middleware-plugin.ts";
|
||||||
|
|
||||||
@@ -10,6 +11,7 @@ export default {
|
|||||||
cssInjectPlugin,
|
cssInjectPlugin,
|
||||||
jsInjectPlugin,
|
jsInjectPlugin,
|
||||||
cssInjectPluginAsync,
|
cssInjectPluginAsync,
|
||||||
|
linkInjectPlugin,
|
||||||
routePlugin({ title: "Title Set From Plugin Config" }),
|
routePlugin({ title: "Title Set From Plugin Config" }),
|
||||||
secondMiddlewarePlugin(),
|
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,
|
body,
|
||||||
'<style id="def">h1 { text-decoration: underline; }</style>',
|
'<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 () => {
|
Deno.test("/with-island prerender", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user