Skip to content

Commit

Permalink
feat: attempt to add support for server
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyaellie committed May 23, 2023
1 parent ed518f2 commit 75a14db
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# build output
dist/
worker/
# generated types
.astro/

Expand Down
31 changes: 22 additions & 9 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ import prefetch from '@astrojs/prefetch';

import cloudflare from '@astrojs/cloudflare';

const isWorker = process.argv.includes('--worker');

// https://astro.build/config
export default defineConfig({
integrations: [tailwind(), react(), prefetch()],
output: 'hybrid',
experimental: {
hybridOutput: true,
assets: true,
},
adapter: cloudflare(),
});
export default defineConfig(
isWorker
? {
output: 'hybrid',
adapter: cloudflare({
mode: 'advanced',
}),
outDir: 'worker',
srcDir: 'cloudflare',
experimental: {
hybridOutput: true,
},
}
: {
integrations: [tailwind(), react(), prefetch()],
experimental: {
assets: true,
},
}
);
4 changes: 4 additions & 0 deletions build/_routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": 1,
"include": ["/api/*"]
}
11 changes: 11 additions & 0 deletions build/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { execSync as exec } from 'child_process';
import { promises as fs } from 'fs';

const build = async () => {
exec('pnpm astro build');
exec('pnpm astro build --worker');
await fs.copyFile('./build/_routes.json', './dist/_routes.json');
await fs.copyFile('./worker/_worker.js', './dist/_worker.js');
};

build();
2 changes: 2 additions & 0 deletions cloudflare/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="astro/client" />
/// <reference path="../.astro/types.d.ts" />
8 changes: 8 additions & 0 deletions cloudflare/pages/api/newsletterSignup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { APIRoute } from 'astro';

export const prerender = false

export const get: APIRoute = async ({ params, request }) => {
console.log('blah', params, request);
return Response.redirect('https://www.google.com');
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"build": "node build/build.mjs",
"preview": "astro preview",
"astro": "astro"
},
Expand Down

0 comments on commit 75a14db

Please sign in to comment.