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

Parse URLs to remove fragments and query params #350

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions src/service-worker/playground-service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type SessionID = string;
*/
const fileAPIs = new Map<string, Deferred<FileAPI>>();

/**
* The parsed URL object for the service worker's scope.
* The scope never changes for the lifetime of a service worker.
*/
const scopeUrl = new URL(self.registration.scope);

/**
* API exposed to the UI thread via Comlink. The static methods on this class
* become instance methods on SwControllerAPI.
Expand Down Expand Up @@ -128,16 +134,17 @@ const onFetch = (e: FetchEvent) => {
}
};

const parseScopedUrl = (url: string) => {
const scope = self.registration.scope;
const parseScopedUrl = (urlString: string) => {
// URLs in scope will be of the form: {scope}{sessionId}/{filePath}. Scope is
// always a full URL prefix, including a trailing slash. Strip query params or
// else the filename won't match.
const sessionAndPath = url.substring(scope.length).split('?')[0];
// always a full URL prefix, including a trailing slash. Strip query params
// and fragments or else the filename won't match.
const url = new URL(urlString);
const sessionAndPath = url.pathname.substring(scopeUrl.pathname.length);

const slashIndex = sessionAndPath.indexOf('/');
let sessionId, filePath: string | undefined;
if (slashIndex === -1) {
console.warn(`Invalid sample file URL: ${url}`);
console.warn(`Invalid sample file URL: ${urlString}`);
} else {
sessionId = sessionAndPath.slice(0, slashIndex);
filePath = sessionAndPath.slice(slashIndex + 1);
Expand Down