Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev-server): support c.env.ASSETS for CF Pages #23

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export type DevServerOptions = {
Pick<
MiniflareOptions,
'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist'
>
> & {
// Enable `env.ASSETS.fetch()` function for Cloudflare Pages.
assets?: boolean
}
>
}
```
Expand Down
27 changes: 23 additions & 4 deletions packages/dev-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export type DevServerOptions = {
Pick<
MiniflareOptions,
'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist'
>
> & {
// Enable `env.ASSETS.fetch()` function for Cloudflare Pages.
assets?: boolean
}
>
}

Expand Down Expand Up @@ -83,11 +86,27 @@ export function devServer(options?: DevServerOptions): Plugin {
}

getRequestListener(async (request) => {
let bindings = {}
let env = {}
if (mf) {
bindings = await mf.getBindings()
env = await mf.getBindings()
if (options?.cf?.assets) {
env = {
// `env.ASSETS.fetch()` function for Cloudflare Pages.
ASSETS: {
async fetch(input: RequestInfo | URL, init?: RequestInit | undefined) {
try {
return await fetch(new Request(input, init))
} catch (e) {
console.error('Failed to execute ASSETS.fetch: ', e)
return new Response(null, { status: 500 })
}
},
},
...env,
}
}
}
const response = await app.fetch(request, bindings, {
const response = await app.fetch(request, env, {
waitUntil: async (fn) => fn,
passThroughOnException: () => {
throw new Error('`passThroughOnException` is not supported')
Expand Down
16 changes: 16 additions & 0 deletions packages/dev-server/test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ test('Should return 200 response - /stream', async ({ page }) => {
const content = await page.textContent('h1')
expect(content).toBe('Hello Vite!')
})

test('Should serve static files in `public/static`', async ({ page }) => {
const response = await page.goto('/static/hello.json')
expect(response?.status()).toBe(200)

const data = await response?.json()
expect(data['message']).toBe('Hello')
})

test('Should handle `env.ASSETS.fetch` function', async ({ page }) => {
const response = await page.goto('/assets/hello.json')
expect(response?.status()).toBe(200)

const data = await response?.json()
expect(data['message']).toBe('Hello')
})
7 changes: 7 additions & 0 deletions packages/dev-server/test/mock/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Hono } from 'hono'
const app = new Hono<{
Bindings: {
NAME: string
ASSETS: { fetch: typeof fetch }
}
}>()

Expand Down Expand Up @@ -52,4 +53,10 @@ app.get('/stream', () => {
})
})

app.get('/assets/hello.json', async (c) => {
const res = await c.env.ASSETS.fetch(new URL('/static/hello.json', c.req.url))
const data = await res.json()
return c.json(data)
})

export default app
3 changes: 3 additions & 0 deletions packages/dev-server/test/public/static/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Hello"
}
1 change: 1 addition & 0 deletions packages/dev-server/test/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineConfig({
bindings: {
NAME: 'Hono',
},
assets: true,
},
}),
],
Expand Down