-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Cloudflare Pages! #2
Comments
After struggling with multiple different bundlers, I have managed to find a way to do this 🙃
I have used Dev Packagesesbuild
@esbuild-plugins/node-modules-polyfill
wrangler@beta # Used for local previews
@cloudflare/workers-types # Optional Worker
import { createPageRenderer } from 'vite-plugin-ssr'
import '../dist/server/importBuild.js'
const renderPage = createPageRenderer({ isProduction: true })
interface EnvironmentVariables {
EXAMPLE_SECRET: string
}
type FetchFunction = EventContext<EnvironmentVariables, '', unknown>
export default {
async fetch (request: FetchFunction['request'], env: FetchFunction['env']) {
// Keep browser requests happy during testing
if (request.url.includes('favicon')) return new Response('', { status: 200 })
if (request.url.includes('/api/')) {
// TODO: Add your custom /api/* logic here.
return new Response('Ok')
}
// Handle Asset requests
if (request.url.includes('/assets')) return env.ASSETS.fetch(request)
// Otherwise pass to SSR handler
const pageContextInit = {
url: request.url,
fetch: (...args: [RequestInfo, RequestInit]) => fetch(...args)
}
const pageContext = await renderPage(pageContextInit)
const { httpResponse } = pageContext
if (httpResponse) {
const { body, statusCode, contentType } = httpResponse
return new Response(body, {
headers: { 'content-type': contentType },
status: statusCode
})
}
}
} Build Script
const esbuild = require('esbuild')
const { default: nodeModulesPolyfills } = require('@esbuild-plugins/node-modules-polyfill')
esbuild.build({
entryPoints: ['./worker/index.js'],
sourcemap: false,
outfile: './dist/client/_worker.js',
minify: true,
logLevel: 'info',
platform: 'browser',
plugins: [nodeModulesPolyfills()],
format: 'esm',
target: 'es2020',
bundle: true
}).then(() => {
console.log(`Successfully built worker.`)
}).catch(error => {
console.error(`There was an error whilst building this worker:`)
console.error(error)
}) PagesMake sure to set the Build output directory to You will then need to add a "scripts": {
"build": "npm run build:site && npm run build:worker",
"build:site": "vite build && vite build --ssr",
"build:worker": "node build.js",
"serve": "wrangler pages dev ./dist/client"
} You can also preview the site locally, by running the Potential IssuesThe |
Thank you @thetre97, I appreciate this! I'll read it precisely in the next days, so I can improve this package! I'll let you know if I needed anything! |
We better discuss this with you all! please give me your suggestions!
The text was updated successfully, but these errors were encountered: