Skip to content

fix: minimal guide for static assets #13

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

Closed
wants to merge 4 commits into from
Closed

Conversation

wtrocki
Copy link
Member

@wtrocki wtrocki commented Oct 7, 2020

The motivation here is to provide a minimal guide for serving static resources that covered all the issues that we have seen in development. We can merge/adjust this guide and build on top of it with more sophisticated use cases.

@helio-frota
Copy link
Member

maybe it is good to have some item/section about webpack
[?]

@wtrocki
Copy link
Member Author

wtrocki commented Oct 9, 2020

Great idea. Do you want to create issue?

@helio-frota
Copy link
Member

@wtrocki sure #23 👍

@wtrocki
Copy link
Member Author

wtrocki commented Nov 10, 2020

@mhdawson I'm really worried about couple PR's in the repo that are hanging out there for a whi
Would look to help with merging them.

Is it ok to get this one landed? It really covers 100% on what we already discussed on the calls and it can be improved.

@mhdawson
Copy link
Contributor

@wtrocki we have a meeting tomorrow and this is top of the list to go through with the rest of the team so I was thinking we'd discuss there and be in a good position to land. Does that work ?

@wtrocki
Copy link
Member Author

wtrocki commented Nov 10, 2020

Sounds like good plan! Thank you

### Caching

The static middleware does server-side caching.
It lets you do two methods of client-side caching: ETag and Max-Age
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every modification in your code will be creating resources with different file name.

When using bundlers you will explicitly need to generate different file names every time content changes.
Example for webpack (most popular bundler) can be found here:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could mention if hash is used in filename, max age could be as long as a year.

### Caching

The static middleware does server-side caching.
It lets you do two methods of client-side caching: ETag and Max-Age
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cover only max-age - we need to change default argument.
Reference spec: http://expressjs.com/en/resources/middleware/serve-static.html


The static middleware does server-side caching.
It lets you do two methods of client-side caching: ETag and Max-Age
If you configured your static middleware to use client side caching please make sure that
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need that differentiated.


The static middleware does server-side caching.
It lets you do two methods of client-side caching: ETag and Max-Age
If you configured your static middleware to use client side caching please make sure that
Copy link
Member Author

@wtrocki wtrocki Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention that ETag is not required. CacheControl only.
Disable ETAG.

ETAG - difference between usages. We need to dig deeper why not reliable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using express.static() method, we can server static resources directly by specifying the folder name where we have stored our static resources.

Documentation for the middleware can be found in:
https://expressjs.com/en/starter/static-files.html
Copy link
Contributor

@roastlechon roastlechon Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional guidance for why or why not to serve assets out of application

Serving assets out of application

  • Makes application docker images self serving of dependencies (no need to deploy/sync assets to S3 to have application running)
    • No need for dedicate step to sync assets (less devops headache)
    • No need for application code to hard code domain path for dedicated static assets domain
    • Allows relative path for assets for ease of portability
  • DeployReviews could go straight to production (since docker images are self serving, these are not dependent on external resources like S3)
  • Misconception: Assets do not bloat the docker image as they are small in comparison to poorly written docker images which do not appropriately remove unused code. Eg docker node-alpine image vs node.)
  • Assets will already be fronted by Akamai CDN and incur less of a hit to origin.
  • Sabotages h2 push (not able to utilize h2 due to separate domain)

Serving assets out of S3

  • Has benefit of not incurring application origin hit cost
    • Although static assets would still be fronted by Akamai CDN
  • Has issues syncing assets from environment to environment
  • COS migration is harder as application is dependent on specific buckets/domain names for where assets will live.
    • Deploying to specific COS buckets means specific secret and access keys per bucket
    • Deploying to specific COS buckets also means associated akamai rules per bucket

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roastlechon knowing how much effort you have put I would not feel good to apply this change myself. I think it will be the best if you take this over and apply this change directly. Simply because this is exactly what we talked about and I would not say it better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can pull your branch and create new PR if that would be okay by you. I can reference existing comments/feedback in the new PR, while you focus more on the other PRs (graphql)

Copy link
Member Author

@wtrocki wtrocki Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything you will like. Really I haven't done much and amount of knowledge you have brought here is amazing. Feel free to create new PR, close this one - anything that will minimize hassle on your side

@csantanapr
Copy link

You can use this link as a central place for Caching headers https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#Caching

It lets you do two methods of client-side caching: ETag and Max-Age
If you configured your static middleware to use client side caching please make sure that
every modification in your code will be creating resources with different file name.

Copy link
Contributor

@roastlechon roastlechon Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO for me to add section about middlelayers to server (nginx, apache, varnish, cdns, etc)

@roastlechon
Copy link
Contributor

Continued guidance here #39

@roastlechon roastlechon closed this Dec 4, 2020
@wtrocki wtrocki deleted the static-resources branch September 8, 2021 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants