diff --git a/index.js b/index.js index 0fe55898e5697fc6a061299780e163ca4553cb05..e9fb7fea0082c7d3df7b07df21bd99cca6c4957c 100644 --- a/index.js +++ b/index.js @@ -144,29 +144,33 @@ export default function (options = {}) { ); } }, - async emulate() { - const proxy = await getPlatformProxy(options.platformProxy); - const platform = /** @type {App.Platform} */ ({ - env: proxy.env, - context: proxy.ctx, - caches: proxy.caches, - cf: proxy.cf - }); + emulate() { + const getting_platform = (async () => { + const proxy = await getPlatformProxy(options.platformProxy); + const platform = /** @type {App.Platform} */ ({ + env: proxy.env, + context: proxy.ctx, + caches: proxy.caches, + cf: proxy.cf + }); - /** @type {Record} */ - const env = {}; - const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env })); + /** @type {Record} */ + const env = {}; + const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env })); - for (const key in proxy.env) { - Object.defineProperty(env, key, { - get: () => { - throw new Error(`Cannot access platform.env.${key} in a prerenderable route`); - } - }); - } + for (const key in proxy.env) { + Object.defineProperty(env, key, { + get: () => { + throw new Error(`Cannot access platform.env.${key} in a prerenderable route`); + } + }); + } + return { platform, prerender_platform }; + })(); return { - platform: ({ prerender }) => { + platform: async ({ prerender }) => { + const { platform, prerender_platform } = await getting_platform; return prerender ? prerender_platform : platform; } };