Skip to content

Commit

Permalink
fix: base path script in head or body when present
Browse files Browse the repository at this point in the history
  • Loading branch information
rturnq committed Sep 20, 2023
1 parent 4729130 commit 53fb149
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-parents-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Ensure base path script is written in head or body when those elements are present
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/isomorphic-runtime-base-path/test.config.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
14 changes: 8 additions & 6 deletions src/components/vite.marko
Original file line number Diff line number Diff line change
Expand Up @@ -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 += `<script${this.___viteInjectAttrs}>${
this.___viteBaseVar
}=${JSON.stringify(this.___viteBasePath)}</script>`
}
for (let i = lastWrittenEntry; i < writtenEntries; i++) {
let entry = entries[i];
Expand Down Expand Up @@ -62,11 +70,5 @@ $ if (!out.global.___viteRenderAssets) {
}

<__flush_here_and_after__>
<if(input.base && !out.global.___flushedMBP)>
$ out.global.___flushedMBP = true;
$!{`<script${out.global.___viteInjectAttrs}>${
out.global.___viteBaseVar
}=${JSON.stringify(input.base)}</script>`}
</if>
$!{out.global.___viteRenderAssets(input.slot)}
</__flush_here_and_after__>

0 comments on commit 53fb149

Please sign in to comment.