-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
Added support for custom asset paths and mapping #3332
Conversation
Thanks @alexindigo, Solid solution to the problem! I just started looking into how to do exactly this, you saved me tons of time! |
Thanks @JeremyJonas :) We've hit the same issue when it was time to grow up out of dev/demo mode to the real thing :) When it's landed, I'll be playing with custom getAssetPath, to have each page's bundle content hashed. We'll see how it goes. :) |
@alexindigo fixed the linting so that the PR only includes files for your changes 👍 |
Thank you @timneutkens :) |
There is another file to clean up: with-apollo-redux / components / PostList.js |
@alexindigo it's actually failing on the integration tests because of a chromedriver timeout. The linting went through fine 😄 I'll check it out 👍 Edit: tests pass fine now 👍 |
Cool, thanks a lot. :) I’ll rebase when I get to the computer. |
9f23c43
to
69ebdac
Compare
Rebased with the latest canary, so it's just my commit there. Let's see how it goes. |
Btw @timneutkens what's the process for landing pull requests? Thank you. |
Actually, canary itself is broken (at least in Travis) – https://travis-ci.org/zeit/next.js/builds/307099631 |
Yeah, those free CI runners all over the place with something more complex than simple unit tests :) #3338 |
This looks great! Would it be possible to implement |
@rauchg After I dug deeper into the code, I couldn’t find user-friendly way to supply custom cdnPrefix function (since it’s used both on the client and the server, and needed to be supplied after the build step) so I opted for stateless default function that could be overwritten with webpack) and assetMap that could be injected at runtime. And simple assetMap is enough in most cases (as you can see from the linked issues) and for hardcore things supplying custom getAssetPath function is not a big deal. |
I think we've already fix this problem in our static export functionality. There's no need to supply a custom map. Here's an example: https://zeit-docs-dmmiqiafyj.now.sh/docs I wrote this few months back, need to findout how I did this :) |
@arunoda Do you mind to elaborate on what was fixed by static export functionality? This PR solves couple issues:
|
To fix the first issue we don't need specific map. We need to append For the second problem, I rather like to see a core solution where we omit the buildId completely but keep the sha-hash for that file. And mixing that with our Even though, this might fixes the problem in short term, I don't think this is best solution. |
@arunoda Do you mind to provide link to a PR or chunk of code where it solved? I'm not sure we're talking about the same thing. It's definitely not solved in the canary branch, as you can see in the red parts of my PR.
It only solves one of the issues, but doesn't provide enough flexibility for developers to make it work for their deployment process. Like if they need release version instead of content hash of a file. Or they need to use commit hash for all their assets. Ability to inject custom logic for this kind of interaction between the app and the outside world is important for many teams.
This PR doesn't add bulky configuration, just another json object with asset mapping if development team needs it and it works for everybody else without any modifications whatsoever. |
Export a simple Next.js into a static HTML version. Then deploy it into a static hosting service. See how we serve files. Here's how we are doing it:
We need to move what we did in
I completely understand this. I didn't say this not important. My point is this solution is like a hot-fix. Not the real solution we are looking for.
It'll achieve what you wanted. |
It works only for static sites, in our case (and I assume few others out there) static site is not the option, like we have listing for all the real estate properties in the US plus other related pages. Having it all exported as static pages would be somewhat suboptimal :) In the same time we do utilize static export feature (you can see it in my examples here) to get assets structure close to how client side is requesting it, since build (
This will solve one of the problems with inconsistent urls and it's welcome, although breaking, change. Current approach allows to have said functionality without breaking existing apps. Btw, I'm curious why it was chosen to have
Getting rid of build id only solves one issue, it still doesn't allow developers to provide custom attributes for each asset endpoint the app is going to hit. Only custom |
I didn't ask you to use static exports. I asked you to use give it a try and see how we serve JS files directly from S3 (or any static hosting solution) without a custom server. We can use the same technique in everywhere. Then you don't need to have a
In the I'll add this support in coming days.
What custom attributes? I'd like to see a use case? |
I played with If this part could be fixed it'd solve my immediate need for sure.
That will be awesome. Does it mean it will be landed in v4 or we'd have to wait for v5 to became stable?
For example there is requirement (Enterprise is weird) :) not to have So instead of Or there is A/B testing framework that deploys different versions of the app bundles to different paths, like It won't be as easy to incorporate all that use cases into the core, but allowing teams to augment existing behavior will open up your framework to more customers. |
We can do a minor release in 4.x branch. |
Okay. I understand. |
Cool. And it should solve #2053 and #2134 issues as well. @JeremyJonas Would that approach work for your use case? Thanks. |
And that approach requires support for mapping, due to "isomorphic" nature of the code, function is static and being prebuilt for server side and client side use cases and it needs to be fed with actual data like build / commit / a/b testing version. Here comes the mapping. So having map support as part of the original function with guide the devs on how to implement things properly and will allow "simplified" customization when things as |
That proposal should solve my use case; adding complete filenames to server paths (same as static export) and having path control with new The |
Thanks for the comment @JeremyJonas |
@arunoda Is there an ETA for your fix on this? Is there a separate PR/issue to track? Thank you! |
@JeremyJonas @alexindigo It's on the master and I did a new release. Simply keep this PR with the |
@arunoda After pulling in latest canary, I've got new error when starting
|
Looks like related: #3398 |
+ centralized asset path generation
69ebdac
to
f355b03
Compare
@arunoda @timneutkens @rauchg I rebased with fixed canary and it's working now. Please take a look. Thank you. |
Hang on, found missing piece, it fell through cracks due to multiple rebases :) |
Fixed it. |
@alexindigo Thanks for contributing this feature, it'll solve a problem I have with performance when running Next in Lambda@Edge. Hopefully it'll get merged soon 👍 |
This PR pulls generation logic for external assets into a single function. And adds mapping support to allow next.js apps to be used with S3-like CDNs. Also it allows to override that function with custom logic to have more complex asset deploy pipeline (like per asset versioning or content hashing).
Added two examples:
custom-asset-map
– shows how simple mapping could solve S3-like CDN issues (#2053, #2134)custom-asset-path
– shows how to use customgetAssetPath
function in prod setup.All my changes are grouped into this commit – e69e339
Second commit fixes styling issues that somehow leaked into the upstream branch and prevented tests from running
/cc @mtoso @dntrkv @rauchg