Skip to content

Add an option for createStaticAssets to respect the basePath of Next.js #821

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

Merged
merged 1 commit into from
Apr 14, 2025

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Apr 10, 2025

The plan is to use this in cloudflare so that the path it handled by the Workers Assets automatically when a basePath is configured

Copy link

changeset-bot bot commented Apr 10, 2025

⚠️ No Changeset found

Latest commit: 0bd4cb7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

github-actions bot commented Apr 10, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 26.43% 2448 / 9260
🔵 Statements 26.43% 2448 / 9260
🔵 Functions 52.86% 129 / 244
🔵 Branches 73.34% 597 / 814
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/open-next/src/build/createAssets.ts 0% 0% 0% 0% 1-265
Generated in workflow #1145 for commit 0bd4cb7 by the Vitest Coverage Report Action

Copy link

pkg-pr-new bot commented Apr 10, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@821

commit: 0bd4cb7

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

If we do that we'll need to modify this file as well otherwise it will break existing aws deployment https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/build/generateOutput.ts
I'm wondering if the good long term solution would be to actually use that output file instead ?

@vicb
Copy link
Contributor Author

vicb commented Apr 10, 2025

If we do that we'll need to modify this file as well otherwise it will break existing aws deployment https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/build/generateOutput.ts I'm wondering if the good long term solution would be to actually use that output file instead ?

Yes, it's more a short term hack for cloudflare...
From my testing it look like only the _next dir is prefixed and others are not touched.
Has the output file been tested with a basePath?

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

Yeah it is used in sst and other places, but i've missed the useBasePath which should make it do nothing there.
FYI every places that use this function need to use the basePath (this includes public file)

@vicb
Copy link
Contributor Author

vicb commented Apr 10, 2025

I'm not quite sure what the best way to move forward here is?
Should we always use the base path when we copy the assets and update the output file so that the change work for both aws and cloudflare? (I don't fully understand how the output file should be changed)

@conico974
Copy link
Contributor

I'm not 100% sure either, my first idea would be that cloudflare (or anyone else) should use the output file to actually finalize the deployment from the created assets from OpenNext.

For example these 2 lines:

from: ".open-next/assets",
to: nextConfig.basePath ? `_assets${nextConfig.basePath}` : "_assets",
would not be correct in aws if we use the base path in createStaticAssets because files would be at .open-next/assets/basePath and would thus create an _assets/basePath/basePath folder.

@vicb
Copy link
Contributor Author

vicb commented Apr 10, 2025

would not be correct in aws if we use the base path in createStaticAssets because files would be at .open-next/assets/basePath and would thus create an _assets/basePath/basePath folder.

The thing I don't get with this code is that static assets do not seem to be prefixed by the basePath?
I see that only _next/... is

@conico974
Copy link
Contributor

On aws it's on the behavior for cloudfront, not in the bucket themselves

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

@vicb What do you want to do with this one ? I'm fine with adding it like that for now as long as we add some comment about it (it won't have any effect on aws since we won't use useBasePath)

fs.cpSync(
path.join(appBuildOutputPath, ".next/static"),
path.join(outputPath, "_next", "static"),
path.join(outputPath, basePath, "_next", "static"),
{ recursive: true },
);
if (fs.existsSync(appPublicPath)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Everything in here should be under outputPath/basePath

Copy link
Contributor Author

@vicb vicb Apr 12, 2025

Choose a reason for hiding this comment

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

What do you mean by "everything in here"?

Looking at the blog example, path are not re-written. The avatar will not be fetch for the basePath. Only <Image/> and <Link/> will use the base path.

Copy link
Contributor

Choose a reason for hiding this comment

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

Favicon and assets in public should also start with basePath. That's the behavior in next start

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not the behavior I observe on the blog example with next start?

Copy link
Contributor

@conico974 conico974 Apr 12, 2025

Choose a reason for hiding this comment

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

If i add the /blog basePath that's what i get for the blog example
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, you'right.

What I did with the blog example is that I probably used npm preview and make that work.
As the path are nor rewritten, the assets should not be prefixed if you want to access them.
But it must be an overlook in the blog example.

In conclusion, you are right, everything must be prefixed.

Would that work if the copy is unconditionally done to the basePath subDir and the basePath logic is removed from the output file? I think this could be a way to support Cloudflare & AWS?

Copy link
Contributor

Choose a reason for hiding this comment

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

We need to add it to the behavior as well anyway (to avoid intercepting things it should not).
I think we go for your solution for now, just add a comment about it, and try to figure out a way to create an output that could be used by everyone.

I had this idea awhile back to add an outputOverride to be able to compute an output that could be used in each platform easily. This inside something like an open-next.build.ts could be something that could help us in a lot of ways.

@vicb
Copy link
Contributor Author

vicb commented Apr 14, 2025

I updated the PR to add a comment, does it looks good like this?

@vicb vicb requested a review from conico974 April 14, 2025 07:35
Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

LGTM Thanks

@vicb
Copy link
Contributor Author

vicb commented Apr 14, 2025

Thanks Nico!

@vicb vicb merged commit 26cdc59 into main Apr 14, 2025
3 checks passed
@vicb vicb deleted the vb-branch-5 branch April 14, 2025 08:20
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.

2 participants