From 2d3f67de499db361072213f57293c0df0ebaa9aa Mon Sep 17 00:00:00 2001 From: Tristan Lee Date: Wed, 15 May 2024 18:49:09 -0400 Subject: [PATCH] Fetch from wikipedia due to re-entry bug --- .../v7-optimizely-edge/edge-functions/main.js | 28 +++++++------ examples/v7-optimizely-edge/edgio.config.js | 36 ++++++++--------- examples/v7-optimizely-edge/routes.ts | 40 ++++++++++++++++++- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/examples/v7-optimizely-edge/edge-functions/main.js b/examples/v7-optimizely-edge/edge-functions/main.js index f3d7577e8..171c8700f 100644 --- a/examples/v7-optimizely-edge/edge-functions/main.js +++ b/examples/v7-optimizely-edge/edge-functions/main.js @@ -47,24 +47,28 @@ export async function handleHttpRequest(request, context) { // Make a decision using Optimizely for the 'text_direction' feature const decision = userContext.decide('text_direction'); - const textDir = decision.enabled ? 'rtl' : 'ltr'; // Determine text direction based on decision + const textDir = decision.enabled ? 1 : -1; // Determine text direction based on decision console.log(`[OPTIMIZELY] User ID: ${userId}, Text Direction: ${textDir}`); - // Modify the request URL to append the determined text direction as a query parameter - const url = new URL('/', request.url); - url.searchParams.set('dir', textDir); - - // Fetch the `/` URL with the appended query parameter which will render a Next.js server page - // with the text direction applied. `edgio_self` origin is used to fetch the URL from the same - // environment which handles the Next.js routes. + // Fetch the homepage of Wikipedia + const url = new URL('https://en.wikipedia.org'); const response = await fetch(url.toString(), { - edgio: { origin: 'edgio_self' }, + edgio: { origin: 'wikipedia' }, }); + // Update the `` tag with the text direction based on the Optimizely decision + const bodyTagRegex = /]*)>/i; + const bodyTagReplacement = ``; + const body = await response.text(); + const updatedBody = body.replace(bodyTagRegex, bodyTagReplacement); + + // Create a new response with the updated body content + const updatedResponse = new Response(updatedBody, response); + // Add the user ID to the response headers as a cookie to ensure the user experience consistency - const cookie = `${COOKIE_NAME}=${userId}; Path=/; Max-Age=31536000; SameSite=Lax`; - response.headers.append('Set-Cookie', cookie); + // const cookie = `${COOKIE_NAME}=${userId}; Path=/; Max-Age=31536000; SameSite=Lax`; + // updatedResponse.headers.append('Set-Cookie', cookie); - return response; + return updatedResponse; } diff --git a/examples/v7-optimizely-edge/edgio.config.js b/examples/v7-optimizely-edge/edgio.config.js index f59e866ed..66fe43e7e 100644 --- a/examples/v7-optimizely-edge/edgio.config.js +++ b/examples/v7-optimizely-edge/edgio.config.js @@ -57,24 +57,22 @@ module.exports = { }, // If you need to proxy some URLs to an origin instead of your Next.js app, you can configure the origins here: - // origins: [ - // { - // // The name of the backend origin - // name: "origin", - // - // // When provided, the following value will be sent as the host header when connecting to the origin. - // // If omitted, the host header from the browser will be forwarded to the origin. - // override_host_header: "test-origin.edgio.net", - // - // // The list of backend hosts - // hosts: [ - // { - // // The domain name or IP address of the origin server - // location: "test-origin.edgio.net" - // } - // ] - // } - // ], + origins: [ + { + name: 'wikipedia', + override_host_header: 'en.wikipedia.org', + hosts: [ + { + location: 'en.wikipedia.org', + }, + ], + tls_verify: { + use_sni: true, + allow_self_signed_certs: true, + sni_hint_and_strict_san_check: 'en.wikipedia.org', + }, + }, + ], // Options for hosting serverless functions on Edgio // serverless: { @@ -99,4 +97,4 @@ module.exports = { // '**/*', // include all files // '!(**/secrets/**/*)', // except everything in the secrets directory // ], -} +}; diff --git a/examples/v7-optimizely-edge/routes.ts b/examples/v7-optimizely-edge/routes.ts index c6348d64a..3dcd53ae5 100644 --- a/examples/v7-optimizely-edge/routes.ts +++ b/examples/v7-optimizely-edge/routes.ts @@ -5,9 +5,47 @@ import { nextRoutes } from '@edgio/next'; export default new Router() // NextRoutes automatically adds routes for all Next.js pages and their assets - .use(nextRoutes) + //.use(nextRoutes) + .match('/:path*', { + origin: { + set_origin: 'wikipedia', + }, + }) // Add a custom route for the Optimizely Edge Function .match('/optimizely', { edge_function: './edge-functions/main.js', + }) + + .match('/:path*/:file.:ext(js|mjs|css)', { + headers: { + set_response_headers: { + 'cache-control': 'public, max-age=86400', + }, + remove_origin_response_headers: ['set-cookie'], + }, + caching: { + ignore_origin_no_cache: [200], + bypass_client_cache: true, + }, + origin: { + set_origin: 'wikipedia', + }, + }) + + // caching assets + .match('/:path*/:file.:ext(png|ico|svg|jpg|jpeg|gif|ttf|woff|otf)', { + headers: { + set_response_headers: { + 'cache-control': 'public, max-age=86400', + }, + remove_origin_response_headers: ['set-cookie'], + }, + caching: { + ignore_origin_no_cache: [200], + bypass_client_cache: true, + }, + origin: { + set_origin: 'wikipedia', + }, });