-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom worker entrypoint (#828)
- Loading branch information
1 parent
f2e7bc0
commit 78accfd
Showing
16 changed files
with
184 additions
and
36 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,23 @@ | ||
--- | ||
'@cloudflare/next-on-pages': minor | ||
--- | ||
|
||
Add support for custom worker entrypoints. | ||
|
||
Example: | ||
|
||
```ts | ||
import nextOnPagesHandler from '@cloudflare/next-on-pages/fetch-handler'; | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
// do something before running the next-on-pages handler | ||
|
||
const response = await nextOnPagesHandler.fetch(request, env, ctx); | ||
|
||
// do something after running the next-on-pages handler | ||
|
||
return response; | ||
}, | ||
} as ExportedHandler<{ ASSETS: Fetcher }>; | ||
``` |
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 @@ | ||
# Advanced Usage | ||
|
||
## Custom Worker Entrypoint | ||
|
||
Certain use cases may require the ability the control what happens in your Pages project's worker. Observability requirements, for instance, might benefit from being able to intercept console logs, catch uncaught exceptions, or monitor the time spent doing work in the next-on-pages router. | ||
|
||
All of these would require modifying the worker to add some code before and/or after next-on-pages' logic runs. | ||
|
||
To achieve this, next-on-pages exposes an option to use your own worker entrypoint. Within it, you can directly import and use the next-on-pages fetch handler. | ||
|
||
1. Create a handler in your project. | ||
|
||
```ts | ||
// file: ./custom-entrypoint.ts | ||
import nextOnPagesHandler from '@cloudflare/next-on-pages/fetch-handler'; | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
// do something before running the next-on-pages handler | ||
|
||
const response = await nextOnPagesHandler.fetch(request, env, ctx); | ||
|
||
// do something after running the next-on-pages handler | ||
|
||
return response; | ||
}, | ||
} as ExportedHandler<{ ASSETS: Fetcher }>; | ||
``` | ||
|
||
2. Pass the entrypoint argument to the next-on-pages CLI with the path to your handler. | ||
|
||
```sh | ||
npx @cloudflare/next-on-pages --custom-entrypoint=./custom-entrypoint.ts | ||
``` |
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
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
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,9 @@ | ||
import 'server-only'; | ||
|
||
export default { | ||
async fetch() { | ||
throw new Error( | ||
'Invalid invocation of the next-on-pages fetch handler - this method should only be used alongside the --custom-entrypoint CLI option. For more details, see: https://github.com/cloudflare/next-on-pages/blob/main/packages/next-on-pages/docs/advanced-usage.md#custom-entrypoint', | ||
); | ||
}, | ||
} as { fetch: ExportedHandlerFetchHandler<{ ASSETS: Fetcher }> }; |
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": "@cloudflare/next-on-pages-tsconfig/tsconfig.json", | ||
"include": ["src/fetch-handler"], | ||
"compilerOptions": { | ||
"emitDeclarationOnly": true, | ||
"declaration": true, | ||
"declarationMap": true, | ||
"outDir": "dist/fetch-handler" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
pages-e2e/features/customEntrypoint/assets/custom-entrypoint.ts
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,11 @@ | ||
import nextOnPagesHandler from '@cloudflare/next-on-pages/fetch-handler'; | ||
|
||
export default { | ||
async fetch(...args) { | ||
const response = await nextOnPagesHandler.fetch(...args); | ||
|
||
response.headers.set('custom-entrypoint', '1'); | ||
|
||
return response; | ||
}, | ||
} as ExportedHandler<{ ASSETS: Fetcher }>; |
12 changes: 12 additions & 0 deletions
12
pages-e2e/features/customEntrypoint/custom-entrypoint.test.ts
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,12 @@ | ||
import { describe, test } from 'vitest'; | ||
|
||
describe('Custom Entrypoint', () => { | ||
test('should set header on response in the worker entrypoint', async ({ | ||
expect, | ||
}) => { | ||
const response = await fetch(`${DEPLOYMENT_URL}/api/hello`); | ||
|
||
await expect(response.text()).resolves.toEqual('Hello world'); | ||
expect(response.headers.get('custom-entrypoint')).toEqual('1'); | ||
}); | ||
}); |
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,3 @@ | ||
{ | ||
"setup": "node --loader tsm setup.ts" | ||
} |
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,2 @@ | ||
import { copyWorkspaceAssets } from '../_utils/copyWorkspaceAssets'; | ||
await copyWorkspaceAssets(); |
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
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
Oops, something went wrong.