Serverless-next.js: Feature - Better static assets support

Created on 14 Jun 2019  路  6Comments  路  Source: serverless-nextjs/serverless-next.js

Original discussion: https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/88

Motivation

Currently it is only possible to host static assets at the top level of the api gw domain, e.g. /robots.txt, /sw.js etc. Nested static routes doesn't work, e.g. /static/bar.png, /static/foo.txt etc.
Also, the existing approach doesn't follow nextjs convention for static/ and public/ folders. Would be better to follow next's convention to ensure feature parity with the development server.

Proposal

Add a catch all /static/{proxy+} route to api gateway that proxies to S3 static assets requests.
Note that static assets will only be possible to host them under the static folder with this approach, which differs from the current api.
Host any files under ./public in the top level path, resources such as robots.txt, favicon.ico etc.
Nextjs docs: (https://github.com/zeit/next.js/#static-file-serving-eg-images)

Caveats

A better approach would be to have a CloudFront distribution in front of API Gateway. API Gateway data transfer out costs is $0.09 per GB whilst CloudFront is $0.085 for the first TB.
A separate feature request will be made to add CloudFront support for production deployments.

enhancement

Most helpful comment

Looks like I'm good to spend a few days on this. Feel free to mark it as assigned to me.

All 6 comments

@mattdell Are you interested to contribute towards this?

@danielcondemarin I鈥檓 willing to take a crack at it. Let me assess my workload on Monday and hopefully I can dedicate some time to this. 馃檪

Looks like I'm good to spend a few days on this. Feel free to mark it as assigned to me.

I've got this working, just need to write some unit tests to finish it off and then I'll raise a PR.

Just to confirm, do we want to keep the existing routes configuration?

    routes:
      - src: ./assets/robots.txt
        path: robots.txt

This scenario is no longer needed as you'd just use the public folder. Also, I've made the static and public folders configurable in serverless.yml so public could be assets if desired.

I'm not sure if routes is in the existing stable version or whether this would be a breaking change. I don't mind either way, but at the moment it seems superfluous.

Just to confirm, do we want to keep the existing routes configuration?

No need to keep that for static routes. Page routes we have to keep for the time being until this lands: https://github.com/zeit/next.js/issues/7607.

I'm not sure if routes is in the existing stable version or whether this would be a breaking change. I don't mind either way, but at the moment it seems superfluous.

Don't worry about making it a breaking change, v2 is still in alpha.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

antoinemaz picture antoinemaz  路  7Comments

futuraprime picture futuraprime  路  6Comments

Vadorequest picture Vadorequest  路  5Comments

sidartha picture sidartha  路  6Comments

krish-dev picture krish-dev  路  5Comments