Description
Right now npx frontity build
generates two types of assets, one for the server and the other for the client.
Server asset:
The server.js
file generated by Webpack for the SSR server.
Users have two options:
- Start a Node server with
npx frontity serve
. - Upload the file to a Serverless service.
Client asset:
The static files generated by Webpack for the client. Like JS files, images, and fonts. They are saved in the /static
folder.
Users have two options:
- Start a Node serve with
npx frontity serve
and keep the/static
folder whereserver.js
can find it. If theserver.js
file receives a request for a URL that contains static (likehttps://domain.com/static/some-file.js
) it will try to find that file in the/static
folder next to it and serve it. - Upload the
/static
folder to a server and configure the domain so the/static
URLs(likehttps://domain.com/static/some-file.js
) serve static files from that folder.
The problem
- Node server
If users run a Node server, then everything is fine. They can use their own server, choose a service like Heroku, use Docker…
- New Serverless hostings
With servers like Now, Netlify, Firebase… that support both static files and serverless functions, everything is fine too. Users just need to indicate the hosting that any request of the folder /static
should return the static assets and any other should run the serverless function. This is usually easy because these hostings are prepared for this type of application.
- Serverless functions
Serverless services like AWS Lambdas, Google Functions or Azure Functions are cheaper than the “New serverless hostings” and it’s still easy to upload and run a serverless function, but configuring the rest of the things needed for a Frontity deploy require a deep knowledge of each platform. These are the steps:
- Upload serverless function.
- Upload static assets.
- Configure an external domain.
- Configure HTTPS for that domain.
- Configure routing so
/static
goes to static assets and the rest to the function. - Add a CDN on top of the domain.
Configuring this is so complex that is the reason for the birth of what I call here the “New Serverless hostings”.
So using a service like AWS, Google Cloud or Azure is not something we can recommend to deploy Frontity in a decoupled manner.
The PHP Theme Bridge deploy
The requirements for the deployment of Frontity when users want to use the PHP Theme Bridge are lower because they don’t need a CDN or an external domain. They become:
- Upload serverless function.
- Upload static assets.
- Configure routing so
/static
goes to static assets and the rest to the function.
For the PHP Theme Bridge, it would be ideal to support services like AWS or Google Cloud because they are in the order of 10x cheaper than the other hostings.
But step 3 is still really complicated.
If we embed the static assets inside the server.js
function, the only requirement for Frontity would be:
- Upload a serverless function.
Which is easy to do.
User Stories
As a PHP Theme Bridge user
I want to avoid having to deploy statics
so that I can use AWS Lambdas and Google Functions easily
Possible solution
I’ve done a proof of concept that successfully embeds plain files (js, txt) and images (png) in the server.js
file and then serves it: