diff --git a/examples/polyfill_io-replace/edge-functions/main.js b/examples/polyfill_io-replace/edge-functions/main.js index f45bf5fab..36e9294fb 100644 --- a/examples/polyfill_io-replace/edge-functions/main.js +++ b/examples/polyfill_io-replace/edge-functions/main.js @@ -6,17 +6,34 @@ export async function handleHttpRequest(request, context) { '//polyfill.io', ]; - const transformerDefinitions = srcPrefixes.map((prefix) => ({ - selector: `script[src^="${prefix}"]`, - element: async (el) => { - const src = el.get_attribute('src'); - el.set_attribute('src', src.replace(prefix, baseReplacementUrl)); + const transformerDefinitions = srcPrefixes.flatMap((prefix) => [ + // Replace the polyfill.io script tag(s) with the new CDN URL + { + selector: `script[src^="${prefix}"]`, + element: async (el) => { + const src = el.get_attribute('src'); + el.set_attribute( + 'src', + src.replace(prefix, `${baseReplacementUrl}/polyfill`) + ); + }, }, - })); + + // Replace the polyfill.io link tag(s) with the new CDN URL + { + selector: `link[href^="${prefix}"]`, + element: async (el) => { + const src = el.get_attribute('href'); + el.set_attribute('href', src.replace(prefix, baseReplacementUrl)); + }, + }, + ]); // Get the HTML from static test file and stream the response body through the HtmlTransformer - return fetch('/polyfill.io-usage', { edgio: { origin: 'edgio_self' } }).then( - (response) => HtmlTransformer.stream(transformerDefinitions, response) + return fetch('/polyfill.io-example', { + edgio: { origin: 'edgio_self' }, + }).then((response) => + HtmlTransformer.stream(transformerDefinitions, response) ); // Use the following code to serve the original content from your origin server diff --git a/examples/polyfill_io-replace/public/polyfill.io.html b/examples/polyfill_io-replace/public/polyfill.io.html index 8c952c664..d85a886e6 100644 --- a/examples/polyfill_io-replace/public/polyfill.io.html +++ b/examples/polyfill_io-replace/public/polyfill.io.html @@ -5,7 +5,8 @@ Sample Page with Polyfill.io - + + diff --git a/examples/polyfill_io-replace/routes.js b/examples/polyfill_io-replace/routes.js index 987996cc0..c79258ad7 100644 --- a/examples/polyfill_io-replace/routes.js +++ b/examples/polyfill_io-replace/routes.js @@ -9,7 +9,7 @@ export default new Router() }) // Serve a static HTML file that includes the polyfill.io script tags - .match('/polyfill.io-usage', ({ serveStatic }) => { + .match('/polyfill.io-example', ({ serveStatic }) => { serveStatic('public/polyfill.io.html'); })