-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
169ca82
commit f95ace9
Showing
3 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }; | ||
} |