-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SSR adaptor for cloudflare pages functions
- Loading branch information
Showing
8 changed files
with
194 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/cloudflare': minor | ||
--- | ||
|
||
add SSR adaptor for Cloudflare Pages functions |
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,24 @@ | ||
# @astrojs/cloudflare | ||
|
||
An SSR adapter for use with Cloudflare Pages Functions targets. Write your code in Astro/Node and deploy to Cloudflare Pages. | ||
|
||
In your astro.config.mjs use: | ||
|
||
```js | ||
import { defineConfig } from 'astro/config'; | ||
import cloudflare from '@astrojs/cloudflare'; | ||
|
||
export default defineConfig({ | ||
adapter: cloudflare() | ||
}); | ||
``` | ||
|
||
## Enabling Preview | ||
|
||
In order for preview to work you must install `wrangler` | ||
|
||
```sh | ||
$ pnpm install wrangler --save-dev | ||
``` | ||
|
||
It's then possible to update the preview script in your `package.json` to `"preview": "wrangler pages dev ./dist"` |
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,33 @@ | ||
{ | ||
"name": "@astrojs/cloudflare", | ||
"description": "Deploy your site to cloudflare pages functions", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"author": "withastro", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/withastro/astro.git", | ||
"directory": "packages/integrations/cloudflare" | ||
}, | ||
"bugs": "https://github.com/withastro/astro/issues", | ||
"homepage": "https://astro.build", | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./server.js": "./dist/server.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"scripts": { | ||
"build": "astro-scripts build \"src/**/*.ts\" && tsc", | ||
"build:ci": "astro-scripts build \"src/**/*.ts\"", | ||
"dev": "astro-scripts dev \"src/**/*.ts\"" | ||
}, | ||
"dependencies": { | ||
"esbuild": "^0.14.42" | ||
}, | ||
"devDependencies": { | ||
"astro": "workspace:*", | ||
"astro-scripts": "workspace:*" | ||
} | ||
} |
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,73 @@ | ||
import type { AstroAdapter, AstroConfig, AstroIntegration, BuildConfig } from 'astro'; | ||
import esbuild from 'esbuild'; | ||
import * as fs from 'fs'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
export function getAdapter(): AstroAdapter { | ||
return { | ||
name: '@astrojs/cloudflare', | ||
serverEntrypoint: '@astrojs/cloudflare/server.js', | ||
exports: ['default'], | ||
}; | ||
} | ||
|
||
export default function createIntegration(): AstroIntegration { | ||
let _config: AstroConfig; | ||
let _buildConfig: BuildConfig; | ||
|
||
return { | ||
name: '@astrojs/cloudflare', | ||
hooks: { | ||
'astro:config:done': ({ setAdapter, config }) => { | ||
setAdapter(getAdapter()); | ||
_config = config; | ||
}, | ||
'astro:build:start': ({ buildConfig }) => { | ||
_buildConfig = buildConfig; | ||
buildConfig.serverEntry = '_worker.js'; | ||
buildConfig.client = new URL('./static/', _config.outDir); | ||
buildConfig.server = new URL('./', _config.outDir); | ||
}, | ||
'astro:build:setup': ({ vite, target }) => { | ||
if (target === 'server') { | ||
vite.resolve = vite.resolve || {}; | ||
vite.resolve.alias = 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 as Record<string, string>)[alias.find] = alias.replacement; | ||
} | ||
} | ||
|
||
vite.ssr = { | ||
target: 'webworker', | ||
noExternal: true, | ||
}; | ||
} | ||
}, | ||
'astro:build:done': async () => { | ||
const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server); | ||
const pkg = fileURLToPath(entryUrl); | ||
|
||
await esbuild.build({ | ||
target: 'es2020', | ||
platform: 'browser', | ||
entryPoints: [pkg], | ||
outfile: pkg, | ||
allowOverwrite: true, | ||
format: 'esm', | ||
bundle: true, | ||
minify: true, | ||
}); | ||
|
||
// throw the server folder in the bin | ||
const chunksUrl = new URL('./chunks', _buildConfig.server); | ||
await fs.promises.rm(chunksUrl, { recursive: true, force: true }); | ||
}, | ||
}, | ||
}; | ||
} |
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,34 @@ | ||
import './shim.js'; | ||
|
||
import type { SSRManifest } from 'astro'; | ||
import { App } from 'astro/app'; | ||
|
||
type Env = { | ||
ASSETS: { fetch: (req: Request) => Promise<Response> }; | ||
}; | ||
|
||
export function createExports(manifest: SSRManifest) { | ||
const app = new App(manifest); | ||
|
||
const fetch = async (request: Request, env: Env) => { | ||
const { origin, pathname } = new URL(request.url); | ||
|
||
// static assets | ||
if (manifest.assets.has(pathname)) { | ||
const assetRequest = new Request(`${origin}/static${pathname}`, request); | ||
return env.ASSETS.fetch(assetRequest); | ||
} | ||
|
||
if (app.match(request)) { | ||
return app.render(request); | ||
} | ||
|
||
// 404 | ||
return new Response(null, { | ||
status: 404, | ||
statusText: 'Not found', | ||
}); | ||
}; | ||
|
||
return { default: { fetch } }; | ||
} |
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,4 @@ | ||
(globalThis as any).process = { | ||
argv: [], | ||
env: {}, | ||
}; |
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,10 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"include": ["src"], | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"module": "ES2020", | ||
"outDir": "./dist", | ||
"target": "ES2020" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.