From 53fb1494c04fde3b99f5f62d445ffd1b2550a77e Mon Sep 17 00:00:00 2001 From: Ryan Turnquist Date: Wed, 20 Sep 2023 13:27:18 -0700 Subject: [PATCH] fix: base path script in head or body when present --- .changeset/wet-parents-occur.md | 5 +++++ .../isomorphic-runtime-base-path/test.config.ts | 17 +++++++++++++++++ src/components/vite.marko | 14 ++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 .changeset/wet-parents-occur.md diff --git a/.changeset/wet-parents-occur.md b/.changeset/wet-parents-occur.md new file mode 100644 index 0000000..af604d5 --- /dev/null +++ b/.changeset/wet-parents-occur.md @@ -0,0 +1,5 @@ +--- +"@marko/vite": patch +--- + +Ensure base path script is written in head or body when those elements are present diff --git a/src/__tests__/fixtures/isomorphic-runtime-base-path/test.config.ts b/src/__tests__/fixtures/isomorphic-runtime-base-path/test.config.ts index 68f6463..b82aadd 100644 --- a/src/__tests__/fixtures/isomorphic-runtime-base-path/test.config.ts +++ b/src/__tests__/fixtures/isomorphic-runtime-base-path/test.config.ts @@ -1,7 +1,24 @@ import type { Options } from "../../.."; +import assert from "assert"; + +async function hasScript(pattern: RegExp) { + const headScripts = await page.locator("head > script"); + const len = await headScripts.count(); + for (let i = 0; i < len; i++) { + const script = await headScripts.nth(i).innerHTML(); + if (pattern.test(script)) { + return true; + } + } + return false; +} export const ssr = true; export async function steps() { + assert( + await hasScript(/\$mbp[a-z0-1-_\s]*=/i), + "Base path script not found in head" + ); await page.click("#clickable"); } export const options: Options = { diff --git a/src/components/vite.marko b/src/components/vite.marko index f4a718b..0306c57 100644 --- a/src/components/vite.marko +++ b/src/components/vite.marko @@ -7,6 +7,14 @@ static function renderAssets(slot) { const lastWrittenEntry = this[slotWrittenEntriesKey] || 0; const writtenEntries = (this[slotWrittenEntriesKey] = entries.length); + if(this.___viteBasePath && !this.___flushedMBP && slot !== "head-prepend") { + this.___flushedMBP = true; + + html += `${ + this.___viteBaseVar + }=${JSON.stringify(this.___viteBasePath)}` + } + for (let i = lastWrittenEntry; i < writtenEntries; i++) { let entry = entries[i]; @@ -62,11 +70,5 @@ $ if (!out.global.___viteRenderAssets) { } <__flush_here_and_after__> - - $ out.global.___flushedMBP = true; - $!{`${ - out.global.___viteBaseVar - }=${JSON.stringify(input.base)}`} - $!{out.global.___viteRenderAssets(input.slot)}