Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@startup-api/cloudflare",
"version": "0.4.4",
"version": "0.4.5",
"license": "Apache-2.0",
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion public/users/accounts.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<power-strip providers="{{ssr:providers}}" style="position: absolute; top: 0; right: 0; z-index: 9999; border-radius: 0 0 0 0.3rem">
<svg viewBox="0 0 24 24" style="width: 1rem; height: 1rem"><path d="M7 2v11h3v9l7-12h-4l4-8z" fill="#ffcc00" /></svg>
</power-strip>
<script src="/users/power-strip.js" async></script>
<script type="module" src="/users/power-strip.js" async></script>

<div class="header-area">
<a href="/" class="back-link">← Back to Home</a>
Expand Down
2 changes: 1 addition & 1 deletion public/users/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
<power-strip providers="{{ssr:providers}}" style="position: absolute; top: 0; right: 0; z-index: 9999; border-radius: 0 0 0 0.3rem">
<svg viewBox="0 0 24 24" style="width: 1rem; height: 1rem"><path d="M7 2v11h3v9l7-12h-4l4-8z" fill="#ffcc00" /></svg>
</power-strip>
<script src="/users/power-strip.js" async></script>
<script type="module" src="/users/power-strip.js" async></script>

<h1>StartupAPI Admin</h1>

Expand Down
23 changes: 8 additions & 15 deletions public/users/power-strip.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,15 @@ class PowerStrip extends HTMLElement {
}

detectBasePath() {
const script =
document.currentScript ||
(function () {
const scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1];
})();

if (script && script.src) {
try {
const url = new URL(script.src);
return url.pathname.substring(0, url.pathname.lastIndexOf('/'));
} catch (e) {
console.error('Failed to parse script URL', e);
}
// This file is loaded as an ES module (`<script type="module">`), so `document.currentScript`
// is null. `import.meta.url` is the module's own URL and is the reliable way to locate ourselves.
try {
const url = new URL(import.meta.url);
return url.pathname.substring(0, url.pathname.lastIndexOf('/'));
} catch (e) {
console.error('Failed to derive base path from import.meta.url', e);
return '';
}
return '';
}

async connectedCallback() {
Expand Down
2 changes: 1 addition & 1 deletion public/users/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<power-strip providers="{{ssr:providers}}" style="position: absolute; top: 0; right: 0; z-index: 9999; border-radius: 0 0 0 0.3rem">
<svg viewBox="0 0 24 24" style="width: 1rem; height: 1rem"><path d="M7 2v11h3v9l7-12h-4l4-8z" fill="#ffcc00" /></svg>
</power-strip>
<script src="/users/power-strip.js" async></script>
<script type="module" src="/users/power-strip.js" async></script>

<div class="header-area">
<a href="/" class="back-link">← Back to Home</a>
Expand Down
2 changes: 1 addition & 1 deletion src/PowerStrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function injectPowerStrip(response: Response, usersPath: string, pr
element(element) {
// The script is always needed to define the <power-strip> custom element.
// It is loaded from the USERS_PATH, which is intercepted by this worker.
element.prepend(`<script src="${usersPath}power-strip.js" async></script>`, { html: true });
element.prepend(`<script type="module" src="${usersPath}power-strip.js" async></script>`, { html: true });

// Defer the component decision until the end of <body>, by which point
// the streaming parser has seen any author-supplied <power-strip>.
Expand Down
2 changes: 1 addition & 1 deletion test/manual/power-strip-theme.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ <h1>Power Strip — color theme preview</h1>
</div>

<!-- Loads the real component. Relative path resolves from test/manual/ to public/users/. -->
<script src="../../public/users/power-strip.js"></script>
<script type="module" src="../../public/users/power-strip.js"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions test/powerstrip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('injectPowerStrip', () => {
const res = await injectPowerStrip(htmlResponse('<html><body><h1>Hi</h1></body></html>'), USERS_PATH, PROVIDERS);
const html = await res.text();

expect(html).toContain(`<script src="${USERS_PATH}power-strip.js" async></script>`);
expect(html).toContain(`<script type="module" src="${USERS_PATH}power-strip.js" async></script>`);
expect(html).toContain(DEFAULT_STRIP_STYLE);
expect(html).toContain('<power-strip providers="google,twitch,patreon"');
expect(countPowerStrips(html)).toBe(1);
Expand All @@ -35,7 +35,7 @@ describe('injectPowerStrip', () => {
);
const html = await res.text();

expect(html).toContain(`<script src="${USERS_PATH}power-strip.js" async></script>`);
expect(html).toContain(`<script type="module" src="${USERS_PATH}power-strip.js" async></script>`);
// No default strip appended, and the author's element is left in place.
expect(html).not.toContain(DEFAULT_STRIP_STYLE);
expect(countPowerStrips(html)).toBe(1);
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('injectPowerStrip', () => {
);
const html = await res.text();

expect(html).toContain(`<script src="${USERS_PATH}power-strip.js" async></script>`);
expect(html).toContain(`<script type="module" src="${USERS_PATH}power-strip.js" async></script>`);
expect(html).not.toContain(DEFAULT_STRIP_STYLE);
expect(html).toContain('providers="google,twitch,patreon"');
});
Expand Down