Skip to content
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

Set up /website Vercel deployment #3092

Merged
merged 23 commits into from
Dec 30, 2020
Merged

Set up /website Vercel deployment #3092

merged 23 commits into from
Dec 30, 2020

Conversation

taneliang
Copy link
Member

@taneliang taneliang commented Dec 28, 2020

#3098.

Goal

Setting up Vercel now, which may be able to replace Netlify (though we're on the old free plan that has unlimited team members but only a meagre 2 build hours) and even our Docker website setup.

The commit history is rather messed up -- it'll be easier to review this just by looking at the diff. Also ignore the changes to the CircleCI config; that has been spun out to #3094 to reduce the mess in this PR. #3094 has been merged

Breakdown

  • Fix Vercel build failure caused by Packtracker plugin.

  • Overhaul scripts/build.js to parallelize website and timetable-only builds. There's now a ~15% reduction in build time when built locally.

  • Nest timetable-only website within the main website's dist folder. This will allow one Vercel project to serve both projects. (Edit: done: https://nusmods-website-git-eliang-vercel.nusmodifications.vercel.app/timetable-only)

  • Configure caching headers.

    • Move long-lived assets into an assets folder.
    • Configure Vercel to provide long-lived caching headers for everything in assets.

    image

This should only be merged until after we can confirm that the export server can consume the new timetable-only bundle hosted by Vercel. (EDIT: it can! 🎉 see #3095)

What's purposely not done

  • We'll no longer proxy /api to api.nusmods.com. Visiting /api in the browser still works as React Router has a redirect configured, but it won't be redirected at the proxy level as imo it's unnecessary. /website will use api.nusmods.com directly. EDIT: we'll redirect /api to api.nusmods.com since it's convenient.

@vercel
Copy link

vercel bot commented Dec 28, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

nusmods-website – ./website

🔍 Inspect: https://vercel.com/nusmodifications/nusmods-website/q9f8zf3vo
✅ Preview: https://nusmods-website-git-eliang-vercel.nusmodifications.vercel.app

nusmods-export – ./export

🔍 Inspect: https://vercel.com/nusmodifications/nusmods-export/mwgktcnya
✅ Preview: https://nusmods-export-git-eliang-vercel.nusmodifications.vercel.app

@ZhangYiJiang
Copy link
Member

I wonder if we can replace this with the GitHub action or CircleCI orb - https://github.com/packtracker/report

@taneliang
Copy link
Member Author

Great idea! Let me try configuring a CircleCI Orb

1. Use new executors key.

2. Also: "workflows no longer have a version number. Make sure to take
   the version key out of your workflows section or bad things will
   happen. The only version key in 2.1 is the top-level key."
   -- https://discuss.circleci.com/t/circleci-2-1-config-overview/26057
Don't use GitHub Action because (I believe) that'll need to rebuild
website again, and we don't want to diversify our CI over too many
separate systems as that'll make it harder to reason about.
So it's a job that builds and runs independently of our build job...
this isn't looking good.
Their docs were a little inconsistent
@taneliang
Copy link
Member Author

So I think we can't use either the orb or the GitHub Action:

  1. Both the orb and the action build the app directly from the Webpack config provided to it, independently of our build script. Apart from just being wasteful and requiring more CI time, our prod build script also requires the browser warning bundle to be built first.
  2. The orb is erroring because it tries to look for a package.json before cding into our project_root 🤦

I'll add the Webpack plugin back to our Webpack config after lunch.

@taneliang taneliang marked this pull request as draft December 29, 2020 08:14
@taneliang taneliang changed the title Parallelize build script, update CircleCI config to V2.1, fix Packtracker error on Vercel Set up /website Vercel deployment Dec 29, 2020
Irrelevant again, now that all targets will be built and used by the
same worker.
@taneliang
Copy link
Member Author

We currently redirect /news to https://blog.nusmods.com in our Caddyfile; I'll add that to our vercel.json tomorrow, but this PR is ready for review.

@@ -2,7 +2,7 @@
"brandName": "NUSMods",
"defaultDescription": "NUSMods is a timetable builder and knowledge platform, providing students with a better way to plan their school timetable and useful module-related information that are community-driven.",
"academicYear": "2020/2021",
"apiBaseUrl": "https://nusmods.com/api",
"apiBaseUrl": "https://api.nusmods.com",
Copy link
Member

Choose a reason for hiding this comment

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

Should add this as a DNS prefetch into either our website or our headers

Copy link
Member Author

Choose a reason for hiding this comment

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

Never knew this existed! But because we already preload the module list endpoint and thus presumably already perform this DNS resolution very early, will this have any significant effect? I'm not too familiar with prefetching in general, but if the improvement is negligible I'm concerned that the cost of sending down that additional DNS prefetching line will negate any gains. Plus there's some (minimal) eng cost to adding more fields to our HTML Webpack Plugin config.

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking Vercel would have a config for that in the headers field

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah we can add a header to our Vercel config, but I'm still not sure if it's worth it. It'll still be one extra line sent down to the client, plus now we'll have api.nusmods.com in multiple places in our codebase. Very minimal issues of course, but if the gains are negligible these may cost more

"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
Copy link
Member

Choose a reason for hiding this comment

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

I think 7 days (604800) is the most I would cache this for. We've definitely had bugs in this file before, and it would be awkward for this file to be borked for an entire year. Also I just took another look at the file and it's somehow 164kb (!?) will investigate

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually hold on, all the files in the /assets/ folder have content hashes in their filenames. I think we potentially may have a problem if we accidentally produce unhashed files, but I think that'll be unlikely?

My intention with this 1 year cache is to help with our "Error loading chunk xxx" errors, by basically letting the CDN serve them even after they're outdated. Maybe 1 year is too long, but given that students tend to keep NUSMods open for a really long time I think 1 week may be a bit too short. Maybe a month?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed it to a month. Let's discuss this again if there's a better choice :)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why you'd think the browser cache would help with the error? We've always kept the older versions of our assets on our current server and it never seem to help. But yeah the URL must contain hash, in which case a 1 year expiration date actually make sense and we can use immutable https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Revalidation_and_reloading

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm thinking the proxy cache/CDN (i.e. Vercel) can help with this. I'm not sure if our previous Caddy/Traefik set up was ever configured to continue serving cached versions of these files after they were deleted by the build script, but I doubt they were. I know the pre-Docker setup had all the old files sitting around, but I don't recall whether we had these errors then.

And yep this uses immutable. I'll bump the max-age back to 1 year in another PR

]);
const errors = results.filter((r) => r.status === 'rejected').map((r) => r.reason);
if (errors.length > 0) {
throw errors;
Copy link
Member

Choose a reason for hiding this comment

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

A bit awkward since you're throwing an array not an Error, but not a huge issue

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'm a bit desensitized to this now after seeing React throw *promises* 😆

Figured it should be fine since we do want to keep track of multiple errors, and creating an Errors extends Error class seemed a bit overkill since these thrown errors are only used by this file.

website/scripts/build.js Outdated Show resolved Hide resolved
website/scripts/build.js Outdated Show resolved Hide resolved
@taneliang
Copy link
Member Author

/news and /api now redirect to blog.nusmods.com and api.nusmods.com respectively, with a 307 status code. Merging now to unblock #3095, but we can continue discussing the unresolved comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants