Skip to content

Commit

Permalink
error on "base path" with "server build path" collision
Browse files Browse the repository at this point in the history
adding special configurations
  • Loading branch information
AirBorne04 authored and AirBorne04 committed Nov 15, 2022
1 parent d619063 commit 0a16dd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/integrations/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export function get({ params }) {
}
```

## Headers, Redirects and function invocation routes

Cloudflare has support for adding custom [headers](https://developers.cloudflare.com/pages/platform/headers/), configuring static [redirects](https://developers.cloudflare.com/pages/platform/redirects/) and defining which routes should [invoke functions](https://developers.cloudflare.com/pages/platform/functions/function-invocation-routes/). This is handled via the special files called _headers, _redirects and _routes.json which can be placed in the `public` folder to take effect.

The _routes.json file is generated during build to prevent a function invocation on assets paths. This will be overwritten when a custom _routes.json is provided. If that is the case the static files will still be served, but the request will go through a function invocation and will count against the request limits of your Cloudflare plan.

## Troubleshooting

For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
Expand Down
11 changes: 9 additions & 2 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const SHIM = `globalThis.process = {
env: {},
};`;

const SERVER_BUILD_FOLDER = '/$server_build/';

export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;
Expand All @@ -47,7 +49,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
updateConfig({
build: {
client: new URL(`.${config.base}`, config.outDir),
server: new URL('./server/', config.outDir),
server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir),
serverEntry: '_worker.js',
},
});
Expand All @@ -63,6 +65,11 @@ export default function createIntegration(args?: Options): AstroIntegration {
`);
}

if (config.base === SERVER_BUILD_FOLDER) {
throw new Error(`
[@astrojs/cloudflare] \`base: "${SERVER_BUILD_FOLDER}"\` is not allowed. Please change your \`base\` config to something else.`);
}
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
Expand All @@ -86,7 +93,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
// Backwards compat
if (needsBuildConfig) {
buildConfig.client = new URL(`.${_config.base}`, _config.outDir);
buildConfig.server = new URL('./server/', _config.outDir);
buildConfig.server = new URL(`.${SERVER_BUILD_FOLDER}`, _config.outDir);
buildConfig.serverEntry = '_worker.js';
}
},
Expand Down

0 comments on commit 0a16dd9

Please sign in to comment.