Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: base path script in head or body when present #77

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__>
Loading