Skip to content

Commit 75a14db

Browse files
committed
feat: attempt to add support for server
1 parent ed518f2 commit 75a14db

File tree

7 files changed

+49
-10
lines changed

7 files changed

+49
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# build output
22
dist/
3+
worker/
34
# generated types
45
.astro/
56

astro.config.mjs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@ import prefetch from '@astrojs/prefetch';
55

66
import cloudflare from '@astrojs/cloudflare';
77

8+
const isWorker = process.argv.includes('--worker');
9+
810
// https://astro.build/config
9-
export default defineConfig({
10-
integrations: [tailwind(), react(), prefetch()],
11-
output: 'hybrid',
12-
experimental: {
13-
hybridOutput: true,
14-
assets: true,
15-
},
16-
adapter: cloudflare(),
17-
});
11+
export default defineConfig(
12+
isWorker
13+
? {
14+
output: 'hybrid',
15+
adapter: cloudflare({
16+
mode: 'advanced',
17+
}),
18+
outDir: 'worker',
19+
srcDir: 'cloudflare',
20+
experimental: {
21+
hybridOutput: true,
22+
},
23+
}
24+
: {
25+
integrations: [tailwind(), react(), prefetch()],
26+
experimental: {
27+
assets: true,
28+
},
29+
}
30+
);

build/_routes.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": 1,
3+
"include": ["/api/*"]
4+
}

build/build.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { execSync as exec } from 'child_process';
2+
import { promises as fs } from 'fs';
3+
4+
const build = async () => {
5+
exec('pnpm astro build');
6+
exec('pnpm astro build --worker');
7+
await fs.copyFile('./build/_routes.json', './dist/_routes.json');
8+
await fs.copyFile('./worker/_worker.js', './dist/_worker.js');
9+
};
10+
11+
build();

cloudflare/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="astro/client" />
2+
/// <reference path="../.astro/types.d.ts" />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { APIRoute } from 'astro';
2+
3+
export const prerender = false
4+
5+
export const get: APIRoute = async ({ params, request }) => {
6+
console.log('blah', params, request);
7+
return Response.redirect('https://www.google.com');
8+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"dev": "astro dev",
77
"start": "astro dev",
8-
"build": "astro build",
8+
"build": "node build/build.mjs",
99
"preview": "astro preview",
1010
"astro": "astro"
1111
},

0 commit comments

Comments
 (0)