Skip to content

Commit

Permalink
test for bug reproduction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Apr 10, 2024
1 parent 169ca82 commit f95ace9
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/serverless-test-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"private": true,
"type": "module",
"exports": {
".": "./dist/index.js",
"./server.js": "./dist/server.js",
".": "./src/index.js",
"./server.js": "./src/server.js",
"./package.json": "./package.json"
},
"files": [
"dist"
"src"
]
}
85 changes: 85 additions & 0 deletions packages/serverless-test-adapter/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
*
* @returns {import('../src/@types/astro').AstroIntegration}
*/
export default function () {
return {
name: '@test/serverless-test-adapter',
hooks: {
'astro:config:setup': ({ updateConfig, config }) => {
updateConfig({
build: {
client: config.outDir,
server: new URL('./_worker.js/', config.outDir),
serverEntry: 'index.js',
redirects: false,
}
});
},
'astro:config:done': ({ setAdapter }) => {
setAdapter({
name: '@test/serverless-test-adapter',
serverEntrypoint: '@test/serverless-test-adapter/server.js',
exports: ['default'],
supportedAstroFeatures: {
serverOutput: 'stable',
},
});
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
vite.resolve ||= {};
vite.resolve.alias ||= {};

const aliases = [
{
find: 'react-dom/server',
replacement: 'react-dom/server.browser',
},
];

if (Array.isArray(vite.resolve.alias)) {
vite.resolve.alias = [...vite.resolve.alias, ...aliases];
} else {
for (const alias of aliases) {
(vite.resolve.alias)[alias.find] = alias.replacement;
}
}

vite.resolve.conditions ||= [];
// We need those conditions, previous these conditions where applied at the esbuild step which we removed
// https://github.com/withastro/astro/pull/7092
vite.resolve.conditions.push('workerd', 'worker');

vite.ssr ||= {};
vite.ssr.target = 'webworker';
vite.ssr.noExternal = true;

vite.build ||= {};
vite.build.rollupOptions ||= {};
vite.build.rollupOptions.output ||= {};
vite.build.rollupOptions.output.banner ||=
'globalThis.process ??= {}; globalThis.process.env ??= {};';

// Cloudflare env is only available per request. This isn't feasible for code that access env vars
// in a global way, so we shim their access as `process.env.*`. This is not the recommended way for users to access environment variables. But we'll add this for compatibility for chosen variables. Mainly to support `@astrojs/db`
vite.define = {
'process.env': 'process.env',
...vite.define,
};
}
// we thought that vite config inside `if (target === 'server')` would not apply for client
// but it seems like the same `vite` reference is used for both
// so we need to reset the previous conflicting setting
// in the future we should look into a more robust solution
if (target === 'client') {
vite.resolve ||= {};
vite.resolve.conditions ||= [];
vite.resolve.conditions = vite.resolve.conditions.filter(
(c) => c !== 'workerd' && c !== 'worker'
);
}
},
},
};
}
65 changes: 65 additions & 0 deletions packages/serverless-test-adapter/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { App } from 'astro/app';

export function createExports(manifest) {
const app = new App(manifest);

const fetch = async (
request,
env,
context
) => {
const { pathname } = new URL(request.url);

// static assets fallback, in case default _routes.json is not used
if (manifest.assets.has(pathname)) {
return env.ASSETS.fetch(request.url.replace(/\.html$/, ''));
}

const routeData = app.match(request);
if (!routeData) {
// https://developers.cloudflare.com/pages/functions/api-reference/#envassetsfetch
const asset = await env.ASSETS.fetch(
request.url.replace(/index.html$/, '').replace(/\.html$/, '')
);
if (asset.status !== 404) {
return asset;
}
}

Reflect.set(
request,
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);

process.env.ASTRO_STUDIO_APP_TOKEN ??= (() => {
if (typeof env.ASTRO_STUDIO_APP_TOKEN === 'string') {
return env.ASTRO_STUDIO_APP_TOKEN;
}
})();

const locals = {
runtime: {
env: env,
cf: request.cf,
caches,
ctx: {
waitUntil: (promise) => context.waitUntil(promise),
passThroughOnException: () => context.passThroughOnException(),
},
},
};

const response = await app.render(request, { routeData, locals });

if (app.setCookieHeaders) {
for (const setCookieHeader of app.setCookieHeaders(response)) {
response.headers.append('Set-Cookie', setCookieHeader);
}
}

return response;
};

return { default: { fetch } };
}

0 comments on commit f95ace9

Please sign in to comment.