|
| 1 | +import { |
| 2 | + createInstance, |
| 3 | + eventDispatcher, |
| 4 | +} from '@optimizely/optimizely-sdk/dist/optimizely.lite.min.js'; |
| 5 | +import optimizelyDatafile from '../lib/optimizely/datafile.json'; |
| 6 | + |
| 7 | +const CLIENT_ENGINE = 'EDGIO_EF'; |
| 8 | +const COOKIE_NAME = 'optimizely_visitor_id'; |
| 9 | + |
| 10 | +/** |
| 11 | + * An example edge function which forwards the request to the origin. |
| 12 | + * See routes.js for how this function is configured to run for requests to "/". |
| 13 | + */ |
| 14 | + |
| 15 | +export async function handleHttpRequest(request, context) { |
| 16 | + // Fetch user Id from the cookie if available so a returning user from same browser session always sees the same variation. |
| 17 | + const userId = |
| 18 | + request.headers |
| 19 | + .get('Cookie') |
| 20 | + ?.split(';') |
| 21 | + .find((cookie) => cookie.trim().startsWith(`${COOKIE_NAME}=`)) |
| 22 | + ?.split('=')[1] || `user-${new Date().getTime()}`; |
| 23 | + |
| 24 | + console.log(JSON.stringify(context, null, 2)); |
| 25 | + const url = new URL('/', request.url); |
| 26 | + const resp = await fetch(url, { |
| 27 | + edgio: { |
| 28 | + origin: 'edgio_self', |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + // handle the response as needed |
| 33 | + // For example, to inject some html into the body: |
| 34 | + const html = await resp.text(); |
| 35 | + |
| 36 | + // Create Optimizely instance using datafile downloaded at build time. |
| 37 | + const instance = createInstance({ |
| 38 | + datafile: optimizelyDatafile, |
| 39 | + clientEngine: CLIENT_ENGINE, |
| 40 | + eventDispatcher, |
| 41 | + }); |
| 42 | + |
| 43 | + // Return the original HTML if the instance is not created. |
| 44 | + if (!instance) { |
| 45 | + return resp; |
| 46 | + } |
| 47 | + |
| 48 | + await instance.onReady(); |
| 49 | + |
| 50 | + // Create Optimizely User Context |
| 51 | + const userContext = instance.createUserContext(userId.toString()); |
| 52 | + |
| 53 | + // Decide variation for the flag. |
| 54 | + const decision = userContext.decide('foo_flag'); |
| 55 | + |
| 56 | + console.log(`[OPTIMIZELY] userId: ${userId}`); |
| 57 | + console.log( |
| 58 | + `[OPTIMIZELY] flag 'foo_flag' is ${ |
| 59 | + decision.enabled ? 'enabled' : 'disabled' |
| 60 | + } for the user ${userId}` |
| 61 | + ); |
| 62 | + |
| 63 | + // To send the response to the client with the new HTML but the same headers as the origin response: |
| 64 | + return new Response(html, { |
| 65 | + ...resp, |
| 66 | + headers: { |
| 67 | + ...resp.headers, |
| 68 | + 'x-edge-function': 'main.js', |
| 69 | + }, |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +// Check if setTimeout is already available (in case of running in an environment that has it) |
| 74 | + |
| 75 | +let timers = new Map(); |
| 76 | +let nextTimerId = 1; |
| 77 | + |
| 78 | +(function () { |
| 79 | + var timerQueue = []; |
| 80 | + var nextTimerId = 0; |
| 81 | + |
| 82 | + function runTimers() { |
| 83 | + var now = Date.now(); |
| 84 | + var nextCheck = null; |
| 85 | + |
| 86 | + // Run due timers |
| 87 | + for (var i = 0; i < timerQueue.length; i++) { |
| 88 | + var timer = timerQueue[i]; |
| 89 | + if (timer.time <= now) { |
| 90 | + timer.callback.apply(null, timer.args); |
| 91 | + if (timer.repeating) { |
| 92 | + timer.time = now + timer.delay; // schedule next run |
| 93 | + nextCheck = |
| 94 | + nextCheck !== null ? Math.min(nextCheck, timer.time) : timer.time; |
| 95 | + } else { |
| 96 | + timerQueue.splice(i--, 1); // remove non-repeating timer |
| 97 | + } |
| 98 | + } else { |
| 99 | + nextCheck = |
| 100 | + nextCheck !== null ? Math.min(nextCheck, timer.time) : timer.time; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Schedule next check |
| 105 | + if (nextCheck !== null) { |
| 106 | + var delay = Math.max(nextCheck - Date.now(), 0); |
| 107 | + setTimeout(runTimers, delay); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + global.setTimeout = function (callback, delay, ...args) { |
| 112 | + var timerId = ++nextTimerId; |
| 113 | + var timer = { |
| 114 | + id: timerId, |
| 115 | + callback: callback, |
| 116 | + time: Date.now() + delay, |
| 117 | + args: args, |
| 118 | + repeating: false, |
| 119 | + delay: delay, |
| 120 | + }; |
| 121 | + timerQueue.push(timer); |
| 122 | + return timerId; |
| 123 | + }; |
| 124 | + |
| 125 | + global.clearTimeout = function (timerId) { |
| 126 | + for (var i = 0; i < timerQueue.length; i++) { |
| 127 | + if (timerQueue[i].id === timerId) { |
| 128 | + timerQueue.splice(i, 1); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + global.queueMicrotask = function (callback) { |
| 135 | + Promise.resolve() |
| 136 | + .then(callback) |
| 137 | + .catch((err) => |
| 138 | + setTimeout(() => { |
| 139 | + throw err; |
| 140 | + }) |
| 141 | + ); |
| 142 | + }; |
| 143 | + |
| 144 | + setTimeout(runTimers, 0); |
| 145 | +})(); |
| 146 | + |
| 147 | +//@ts-ignore |
| 148 | + |
| 149 | +// export async function middleware(req: NextRequest, ev: NextFetchEvent) { |
| 150 | +// // Fetch user Id from the cookie if available so a returning user from same browser session always sees the same variation. |
| 151 | +// const userId = req.cookies.get(COOKIE_NAME)?.value || crypto.randomUUID() |
| 152 | + |
| 153 | +// // Create Optimizely instance using datafile downloaded at build time. |
| 154 | +// const instance = createInstance({ |
| 155 | +// datafile: optimizelyDatafile, |
| 156 | +// clientEngine: VERCEL_EDGE_CLIENT_ENGINE, |
| 157 | +// eventDispatcher: { |
| 158 | +// dispatchEvent: ({ url, params }: { url: string; params: any }) => { |
| 159 | +// // Tell edge function to wait for this promise to fullfill. |
| 160 | +// ev.waitUntil( |
| 161 | +// fetch(url, { |
| 162 | +// method: 'POST', |
| 163 | +// body: JSON.stringify(params), |
| 164 | +// }) |
| 165 | +// ) |
| 166 | +// }, |
| 167 | +// }, |
| 168 | +// }) |
| 169 | + |
| 170 | +// // Create Optimizely User Context |
| 171 | +// const userContext = instance!.createUserContext(userId.toString()) |
| 172 | + |
| 173 | +// // Decide variation for the flag. |
| 174 | +// const decision = userContext!.decide('product_sort') |
| 175 | + |
| 176 | +// // Fetch datafile revision for debugging. |
| 177 | +// const revision = instance!.getOptimizelyConfig()!.revision |
| 178 | + |
| 179 | +// console.log(`[OPTIMIZELY] Datafile Revision: ${revision}`) |
| 180 | +// console.log(`[OPTIMIZELY] userId: ${userId}`) |
| 181 | +// console.log( |
| 182 | +// `[OPTIMIZELY] flag 'product_sort' is ${ |
| 183 | +// decision.enabled ? 'enabled' : 'disabled' |
| 184 | +// } for the user ${userId}` |
| 185 | +// ) |
| 186 | +// console.log( |
| 187 | +// `[OPTIMIZELY] User ${userId} was bucketed in to variation ${decision.variationKey}` |
| 188 | +// ) |
| 189 | +// console.log(`[OPTIMIZELY] sort_method is ${decision.variables.sort_method}`) |
| 190 | + |
| 191 | +// // Rewriting the path based on sort_method. The default is Alphabetical. |
| 192 | +// req.nextUrl.pathname = |
| 193 | +// decision.variables.sort_method === 'popular_first' ? '/popular' : '/' |
| 194 | +// let res = NextResponse.rewrite(req.nextUrl) |
| 195 | + |
| 196 | +// if (!req.cookies.has(COOKIE_NAME)) { |
| 197 | +// // Saving userId in the cookie so that the decision sticks for subsequent visits. |
| 198 | +// res.cookies.set(COOKIE_NAME, userId) |
| 199 | +// } |
| 200 | + |
| 201 | +// return res |
| 202 | +// } |
0 commit comments