Skip to content

Commit

Permalink
Fetch from wikipedia due to re-entry bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlee85 committed May 15, 2024
1 parent b64b59c commit 2d3f67d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
28 changes: 16 additions & 12 deletions examples/v7-optimizely-edge/edge-functions/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<body>` tag with the text direction based on the Optimizely decision
const bodyTagRegex = /<body([^>]*)>/i;
const bodyTagReplacement = `<body style="transform: scaleX(${textDir});"$1>`;
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;
}
36 changes: 17 additions & 19 deletions examples/v7-optimizely-edge/edgio.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -99,4 +97,4 @@ module.exports = {
// '**/*', // include all files
// '!(**/secrets/**/*)', // except everything in the secrets directory
// ],
}
};
40 changes: 39 additions & 1 deletion examples/v7-optimizely-edge/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});

0 comments on commit 2d3f67d

Please sign in to comment.