From 7ff2781fcd0420b9755c24a634dbb81dd6392c34 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 10 Nov 2023 13:59:41 +0100 Subject: [PATCH] feat: allow plugins to modify HTML body (#2022) Based on https://github.com/denoland/fresh/pull/1769 . Mostly added a test. --------- Co-authored-by: Adam Gregory --- docs/canary/concepts/plugins.md | 4 +++- src/server/render.ts | 8 ++++++++ src/server/types.ts | 2 ++ tests/fixture_plugin_html/deno.json | 12 ++++++++++++ tests/fixture_plugin_html/dev.ts | 5 +++++ tests/fixture_plugin_html/fresh.config.ts | 15 +++++++++++++++ tests/fixture_plugin_html/fresh.gen.ts | 17 +++++++++++++++++ tests/fixture_plugin_html/main.ts | 11 +++++++++++ tests/fixture_plugin_html/routes/index.tsx | 3 +++ tests/plugin_test.ts | 12 ++++++++++++ tests/test_utils.ts | 22 +++++++++++++++++++++- 11 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/fixture_plugin_html/deno.json create mode 100755 tests/fixture_plugin_html/dev.ts create mode 100644 tests/fixture_plugin_html/fresh.config.ts create mode 100644 tests/fixture_plugin_html/fresh.gen.ts create mode 100644 tests/fixture_plugin_html/main.ts create mode 100644 tests/fixture_plugin_html/routes/index.tsx diff --git a/docs/canary/concepts/plugins.md b/docs/canary/concepts/plugins.md index 5c663748..100d4d24 100644 --- a/docs/canary/concepts/plugins.md +++ b/docs/canary/concepts/plugins.md @@ -80,7 +80,9 @@ 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`, `links` and `scripts` arrays in this object. +by adding them to `styles`, `links` and `scripts` arrays in this object. The +plugin can also replace the the HTML in side the ``-element of the page by +including a `htmlText` string 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 diff --git a/src/server/render.ts b/src/server/render.ts index 342b1758..00eb2500 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -276,6 +276,10 @@ export async function render( ); } renderResults.push([plugin, res]); + + if (res.htmlText !== undefined) { + bodyHtml = res.htmlText; + } } else { bodyHtml = renderHtml(renderState); } @@ -308,6 +312,10 @@ export async function render( `The 'renderAsync' function was not called by ${plugin?.name}'s async render hook.`, ); } + + if (res.htmlText !== undefined) { + bodyHtml = res.htmlText; + } } else { await opts.renderFn(ctx, () => renderSync().htmlText); diff --git a/src/server/types.ts b/src/server/types.ts index 08618b94..fce6cc3a 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -601,6 +601,8 @@ export interface PluginRenderResult { scripts?: PluginRenderScripts[]; /** Link tags for the page */ links?: PluginRenderLink[]; + /** Body HTML transformed by the plugin */ + htmlText?: string; } export interface PluginRenderStyleTag { diff --git a/tests/fixture_plugin_html/deno.json b/tests/fixture_plugin_html/deno.json new file mode 100644 index 00000000..95bc3f0c --- /dev/null +++ b/tests/fixture_plugin_html/deno.json @@ -0,0 +1,12 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_plugin_html/dev.ts b/tests/fixture_plugin_html/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_plugin_html/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_plugin_html/fresh.config.ts b/tests/fixture_plugin_html/fresh.config.ts new file mode 100644 index 00000000..7bc58334 --- /dev/null +++ b/tests/fixture_plugin_html/fresh.config.ts @@ -0,0 +1,15 @@ +import { defineConfig } from "$fresh/server.ts"; + +export default defineConfig({ + plugins: [ + { + name: "html inject", + async renderAsync(ctx) { + await ctx.renderAsync(); + return { + htmlText: "

it works

", + }; + }, + }, + ], +}); diff --git a/tests/fixture_plugin_html/fresh.gen.ts b/tests/fixture_plugin_html/fresh.gen.ts new file mode 100644 index 00000000..800cc24a --- /dev/null +++ b/tests/fixture_plugin_html/fresh.gen.ts @@ -0,0 +1,17 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $0 from "./routes/index.tsx"; + +import { Manifest } from "$fresh/server.ts"; + +const manifest = { + routes: { + "./routes/index.tsx": $0, + }, + islands: {}, + baseUrl: import.meta.url, +} satisfies Manifest; + +export default manifest; diff --git a/tests/fixture_plugin_html/main.ts b/tests/fixture_plugin_html/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_plugin_html/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_plugin_html/routes/index.tsx b/tests/fixture_plugin_html/routes/index.tsx new file mode 100644 index 00000000..33b1bcd8 --- /dev/null +++ b/tests/fixture_plugin_html/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Foo() { + return

it does not work

; +} diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index 55abfba1..aa2cb21f 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -14,6 +14,7 @@ import { clickWhenListenerReady, runBuild, startFreshServer, + withFakeServe, withPageName, } from "./test_utils.ts"; @@ -181,3 +182,14 @@ Deno.test("plugin script doesn't halt island execution", async () => { }, ); }); + +Deno.test("supports returning htmlText", async () => { + await withFakeServe( + "./tests/fixture_plugin_html/main.ts", + async (server) => { + const doc = await server.getHtml("/"); + assertEquals(doc.body.textContent, "it works"); + }, + { loadConfig: true }, + ); +}); diff --git a/tests/test_utils.ts b/tests/test_utils.ts index b486bd23..4683e144 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -286,6 +286,7 @@ export async function fakeServe( export async function withFakeServe( name: string, cb: (server: FakeServer) => Promise | void, + options: { loadConfig?: boolean } = {}, ) { const fixture = join(Deno.cwd(), name); const dev = basename(name) === "dev.ts"; @@ -293,7 +294,26 @@ export async function withFakeServe( const manifestPath = toFileUrl(join(dirname(fixture), "fresh.gen.ts")).href; const manifestMod = await import(manifestPath); - const server = await fakeServe(manifestMod.default, { dev }); + const configPath = join(dirname(fixture), "fresh.config.ts"); + + let config = { dev }; + + // For now we load config on a case by case basis, because something in + // twind (unsure) doesn't work well if multiple instances are running + if (options.loadConfig) { + try { + const stats = await Deno.stat(configPath); + if (stats.isFile) { + const m = await import(toFileUrl(configPath).href); + config = m.default; + config.dev = dev; + } + } catch { + // ignore + } + } + + const server = await fakeServe(manifestMod.default, config); await cb(server); }