Deploying SolidStart to Cloudflare Pages
How you can deploy a SolidStart app to Cloudflare Pages.
As of @solidjs/[email protected]
, 7 July 2024.
This is updated as of @solidjs/[email protected]
, 2 April 2024.
Configure the deployment for Cloudflare Pages
Let’s build locally to verify the preset is being used correctly. You should see files in /dist
folder.
Deploy to Cloudflare Pages
I created a repo for this on Github and linked it to a new Pages project on Cloudflare dashboard.
We need to configure these build settings
- Build command:
pnpm run build
- Build output directory:
dist
After deploying, you will notice the build fails. We need to enable node compatiblity.
- Go to Pages project Settings -> Functions -> Compatibility Flags
- Enable:
nodejs_compat
You will need to trigger a new deployment to have the changes. Visit the deployment URL and you should see the page display.
Access Cloudflare environment and bindings
Note that Cloudflare only exposes environment variables in the request
object so using process.env.SECRET
will not work.
To access Cloudflare environment and bindings, we can use getRequestEvent()
.
At this point you will notice you have no types, we need to extend the context type to include the Cloudflare environment.
First install worker types.
Declare your types, I do this in src/global.d.ts
.
Now event.nativeEvent.context
should have the correct types.
A pattern I found useful for accessing database or other api clients is to create a middleware that attaches the client to event.locals
.
You can now access the client in your server actions and API routes.