-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f0c6ee
commit e5bc34e
Showing
6 changed files
with
31 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
import { httpRequestFromWebWorker } from './sw-message'; | ||
import { CacheControl, ContentType, response } from './response'; | ||
import { debug, PT_PROXY_URL } from '../utils'; | ||
import { httpRequestFromWebWorker } from './sw-message'; | ||
import Sandbox from '@sandbox'; | ||
import SandboxHash from '@sandbox-hash'; | ||
import SandboxDebug from '@sandbox-debug'; | ||
|
||
export const onFetchServiceWorkerRequest = (self: ServiceWorkerGlobalScope, ev: FetchEvent) => { | ||
const req = ev.request; | ||
const pathname = new URL(req.url).pathname; | ||
|
||
if (debug && pathname.endsWith('partytown-sandbox.debug')) { | ||
ev.respondWith( | ||
new Response(SandboxDebug, { | ||
headers: { 'content-type': 'text/html', 'Cache-Control': 'no-store' }, | ||
}) | ||
); | ||
} else if (!debug && pathname.endsWith('partytown-sandbox-' + SandboxHash)) { | ||
ev.respondWith( | ||
new Response(Sandbox, { | ||
headers: { | ||
'content-type': 'text/html', | ||
'Cache-Control': 'max-age=31556952, immutable', | ||
}, | ||
}) | ||
); | ||
// debug version (sandbox and web worker are not inlined) | ||
ev.respondWith(response(SandboxDebug, ContentType.HTML, CacheControl.NoStore)); | ||
} else if (!debug && pathname.endsWith('partytown-sandbox')) { | ||
// sandbox and webworker, minified and inlined | ||
ev.respondWith(response(Sandbox, ContentType.HTML, CacheControl.NoStore)); | ||
} else if (pathname.endsWith(PT_PROXY_URL)) { | ||
// proxy request | ||
ev.respondWith(httpRequestFromWebWorker(self, req)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const response = (body: string, contentType: ContentType, cacheControl: CacheControl) => | ||
new Response(body, { | ||
headers: { | ||
'content-type': contentType === ContentType.JSON ? 'application/json' : 'text/html', | ||
'Cache-Control': | ||
cacheControl === CacheControl.Immutable ? 'max-age=31556952,immutable' : 'no-store', | ||
}, | ||
}); | ||
|
||
export const enum ContentType { | ||
HTML, | ||
JSON, | ||
} | ||
|
||
export const enum CacheControl { | ||
Immutable, | ||
NoStore, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters