Skip to content

Commit

Permalink
dispatch urlchange event on client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Mar 4, 2025
1 parent 6e451ee commit fd346e8
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 266 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@
"@nebula-services/bare-server-node": "^2.0.4",
"@playwright/test": "^1.50.1",
"@rsdoctor/rspack-plugin": "^0.4.13",
"@rspack/cli": "^1.2.5",
"@rspack/core": "^1.2.5",
"@rspack/cli": "^1.2.7",
"@rspack/core": "^1.2.7",
"@types/eslint": "^9.6.1",
"@types/estree": "^1.0.6",
"@types/node": "^22.13.5",
"@types/serviceworker": "^0.0.121",
"@typescript-eslint/eslint-plugin": "^8.24.1",
"@typescript-eslint/parser": "^8.24.1",
"@types/node": "^22.13.9",
"@types/serviceworker": "^0.0.123",
"@typescript-eslint/eslint-plugin": "^8.26.0",
"@typescript-eslint/parser": "^8.26.0",
"dotenv": "^16.4.7",
"eslint": "^9.21.0",
"fastify": "^5.2.1",
"playwright": "^1.50.1",
"prettier": "^3.5.1",
"prettier": "^3.5.3",
"tslib": "^2.8.1",
"typescript": "^5.7.3"
"typescript": "^5.8.2"
},
"dependencies": {
"@mercuryworkshop/bare-mux": "^2.1.7",
Expand Down
474 changes: 237 additions & 237 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,16 @@ export class ScramjetClient {

return sframe;
}
get isSubframe(): boolean {
if (!iswindow) return false;
const frame = this.descriptors.get("window.frameElement", this.global);

if (!frame) return false; // we're top level
const sframe = frame[SCRAMJETFRAME];
if (!sframe) return true;

return false;
}
loadcookies(cookiestr: string) {
this.cookieStore.load(cookiestr);
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/dom/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function (client: ScramjetClient, _self: Self) {
ctx.call();

const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev);
if (!client.isSubframe) client.frame?.dispatchEvent(ev);
},
});

Expand All @@ -21,7 +21,7 @@ export default function (client: ScramjetClient, _self: Self) {
ctx.call();

const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev);
if (!client.isSubframe) client.frame?.dispatchEvent(ev);
},
});
}
2 changes: 1 addition & 1 deletion src/client/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class UrlChangeEvent extends Event {
}
}

export class ScramjetContextInit extends Event {
export class ScramjetContextEvent extends Event {
constructor(public window: Self) {
super("contextInit");
}
Expand Down
8 changes: 5 additions & 3 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { loadCodecs } from "../scramjet";
import { SCRAMJETCLIENT } from "../symbols";
import { ScramjetClient } from "./client";
import { ScramjetContextInit } from "./events";
import { ScramjetContextEvent, UrlChangeEvent } from "./events";
import { ScramjetServiceWorkerRuntime } from "./swruntime";

export const iswindow = "window" in self && window instanceof Window;
Expand All @@ -30,8 +30,10 @@ if (!(SCRAMJETCLIENT in <Partial<typeof self>>self)) {
runtime.hook();
}

const ev = new ScramjetContextInit(client.global.window);
client.frame?.dispatchEvent(ev);
const contextev = new ScramjetContextEvent(client.global.window);
client.frame?.dispatchEvent(contextev);
const urlchangeev = new UrlChangeEvent(client.url.href);
if (!client.isSubframe) client.frame?.dispatchEvent(urlchangeev);
}

Reflect.deleteProperty(self, "WASM");
Expand Down
13 changes: 7 additions & 6 deletions src/worker/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function handleFetch(
requesturl.searchParams.delete("dest");
}

if (requesturl.pathname == this.config.files.wasm) {
if (requesturl.pathname === this.config.files.wasm) {
return fetch(this.config.files.wasm).then(async (x) => {
const buf = await x.arrayBuffer();
const b64 = btoa(
Expand All @@ -54,7 +54,8 @@ export async function handleFetch(
);

let payload = "";
payload += `if ("document" in self && document.currentScript) { document.currentScript.remove(); }\n`;
payload +=
"if ('document' in self && document.currentScript) { document.currentScript.remove(); }\n";
payload += `self.WASM = '${b64}';`;

return new Response(payload, {
Expand Down Expand Up @@ -164,11 +165,11 @@ export async function handleFetch(

const ev = new ScramjetRequestEvent(
url,
headers.headers,
request.body,
request.method,
request.destination,
client,
headers.headers
client
);
this.dispatchEvent(ev);

Expand Down Expand Up @@ -396,11 +397,11 @@ export class ScramjetHandleResponseEvent extends Event {
export class ScramjetRequestEvent extends Event {
constructor(
public url: URL,
public requestHeaders: Record<string, string>,
public body: BodyType,
public method: string,
public destination: string,
public client: Client,
public requestHeaders: Record<string, string>
public client: Client
) {
super("request");
}
Expand Down
9 changes: 0 additions & 9 deletions static/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,6 @@ function BrowserApp() {
if (!e.url) return;
this.url = e.url;
});
frame.frame.addEventListener("load", () => {
let url = frame.frame.contentWindow.location.href;
if (!url) return;
if (url === "about:blank") return;

this.url = $scramjet.codec.decode(
url.substring((location.href + "/scramjet").length)
);
});

const handleSubmit = () => {
this.url = this.url.trim();
Expand Down

0 comments on commit fd346e8

Please sign in to comment.