@@ -57,24 +57,46 @@ const embed = `${stackblitz}?embed=1&file=App.tsx&view=both&corp=1`
5757<script >
5858/**
5959 * The StackBlitz editor needs a cross-origin isolated page, which GitHub Pages cannot serve.
60- * The service worker adds the headers, but only to pages opened with `?stackblitz`:
61- * reading an example does not reload the page or change how it loads.
60+ * The service worker adds the headers, but only to pages opened with `?stackblitz` and to the
61+ * previews they embed: reading an example does not reload the page or change how it loads.
6262 */
6363const EDITOR_PARAM = 'stackblitz'
64+ const SERVICE_WORKER = '/coi-serviceworker.js'
65+ const SERVICE_WORKER_TIMEOUT = 5000
66+ // Safari has no credentialless COEP, so its pages cannot be isolated: StackBlitz opens in a new tab there
67+ const canIsolate = 'serviceWorker' in navigator && 'credentialless' in HTMLIFrameElement.prototype
68+
69+ /**
70+ * Registers the service worker and waits until it can serve the next navigation.
71+ * @returns Whether the service worker is active.
72+ */
73+ async function activateServiceWorker() {
74+ try {
75+ await navigator.serviceWorker.register(SERVICE_WORKER)
76+
77+ return await Promise.race([
78+ navigator.serviceWorker.ready.then(() => true),
79+ new Promise<boolean>((resolve) => {
80+ setTimeout(resolve, SERVICE_WORKER_TIMEOUT, false)
81+ })
82+ ])
83+ } catch {
84+ // Registration fails where the site data of the page is blocked
85+ return false
86+ }
87+ }
6488
6589class Example extends HTMLElement {
6690 connectedCallback() {
67- const editorRequested = new URLSearchParams(location.search).has(EDITOR_PARAM)
68-
6991 this.querySelector('[data-open-editor]')?.addEventListener('click', () => {
7092 void this.openEditor()
7193 })
7294
73- if ('serviceWorker' in navigator ) {
74- void navigator.serviceWorker.register('/coi-serviceworker.js' )
95+ if (canIsolate ) {
96+ void activateServiceWorker( )
7597 }
7698
77- if (editorRequested && crossOriginIsolated) {
99+ if (new URLSearchParams(location.search).has(EDITOR_PARAM) && crossOriginIsolated) {
78100 this.querySelectorAll<HTMLElement>('[role="tab"]')[1]?.click()
79101 this.loadEditor()
80102 this.scrollIntoView()
@@ -87,14 +109,16 @@ class Example extends HTMLElement {
87109 return
88110 }
89111
90- // The headers did not apply, as in browsers without credentialless COEP: StackBlitz itself works anyway
91- if (!('serviceWorker' in navigator) || new URLSearchParams(location.search).has(EDITOR_PARAM)) {
92- window.open(this.dataset.stackblitz, '_blank', 'noopener')
112+ // A page that is still not isolated after the reload will not be: open StackBlitz itself
113+ if (
114+ !canIsolate
115+ || new URLSearchParams(location.search).has(EDITOR_PARAM)
116+ || !await activateServiceWorker()
117+ ) {
118+ window.open(this.dataset['stackblitz'], '_blank', 'noopener')
93119 return
94120 }
95121
96- await navigator.serviceWorker.ready
97-
98122 const url = new URL(location.href)
99123
100124 url.searchParams.set(EDITOR_PARAM, '')
@@ -104,7 +128,7 @@ class Example extends HTMLElement {
104128 loadEditor() {
105129 const editor = this.querySelector('[data-editor]')
106130 const iframe = document.createElement('iframe')
107- const url = new URL(this.dataset. embed!)
131+ const url = new URL(this.dataset[' embed'] !)
108132
109133 url.searchParams.set('theme', document.documentElement.dataset['theme'] === 'light' ? 'light' : 'dark')
110134 iframe.className = 'rc2-example-frame'
0 commit comments