Skip to content

feat(nextjs): Support assetPrefix option #6388

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 8 commits into from
Dec 2, 2022

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Dec 2, 2022

This adds support support for the nextjs assetPrefix option to the nextjs SDK. Currently, if users set this option, it changes the filepaths in their client-side stacktraces in a way not reflected in their release artifact names, causing sourcemaps not to work. This fixes that by using RewriteFrames to modify the stacktrace filepaths so that they'll match the release artifacts.

Notes:

  • Initial work on this was done by @tomas-c in fix(nextjs): handle assetPrefix #6241. (Thanks, @tomas-c!) The approach taken there was to change the way the client-side release artifacts are named, rather than to change the filenames in the stacktrace as is done here. After discussions with @lforst, I decided to take this approach because it's more consistent with what we already do on the server side. There, we use RewriteFrames, and we mutate the filepaths to start with app:///_next. This mirrors that for the client side.

  • In the process of working on this, I discovered that we currently have a bug, caused by the way we handle the basePath option when naming release artifacts, including it at the beginning of all artifact names. Unfortunately, it's not included in stacktrace filenames, so this is a mistake.

    On the server side, this makes the stacktrace filenames and the artifact filenames not match, meaning sourcemaps for people using that option are currently broken for all sever-side errors. (The reason this hasn't been more obvious is that in the node SDK, we harvest context lines at capture time, rather than relying on sourcemapping to provide them. Also, server-side built code is transpiled but not bundled or mangled, making even un-sourcemapped code much easier to read than it is on the browser side of things.)

    On the browser side, it doesn't break sourcemaps, but only because it turns out that nextjs copies basePath over into assetPrefix if a user provides the former but not the latter. As mentioned, basePath doesn't appear in stacktrace filepaths, but assetPrefix does, which is what led us to think was working.

    To fix this, this PR stops including basePath in artifact filenames. As a result, on both the server-side and client-side, all artifact filenames are now of the form ~/_next/..., rather than some looking like that but others looking like ~/basePathValue/_next/....

  • Part of the work here was figuring out how distDir, basePath, and assetPrefix interact, and where they show up in stacktrace filepaths. Just so it's written down somewhere, the answer is:

    • basePath - Never shows up in stacktrace filepaths, except insofar as it's copied into assetPrefix if it's set and assetPrefix isn't, in which case assetPrefix acts like it's path-only.
    • distDir - Server-side stacktrace filepaths are of the form <pathToProjectDir>/<distDir>/server/... (or <pathToProjectDir>/<distDir>/serverless/...). Example: /path/on/host/machine/someDistDir/server/....
    • assetPrefix - If assetPrefix is a full url, client-side stacktrace filepaths are of the form <assetPrefixHost>/<assetPrefixPath>/_next/static/.... If assetPrefix just a path, stacktrace filepaths are of the form <host>/<assetPrefixPath>/_next/static/.... Examples: http://some.assetPrefix.host/some/assetPrefix/path/_next/...` and http://some.normal.domain/some/assetPrefix/path/_next/....

Fixes #4174.

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-asssetPrefix-option branch from 80b9957 to 059a9eb Compare December 2, 2022 09:12
@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.57 KB (-0.02% 🔽)
@sentry/browser - ES5 CDN Bundle (minified) 60.59 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.36 KB (+0.02% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 54.15 KB (0%)
@sentry/browser - Webpack (gzipped + minified) 20.12 KB (0%)
@sentry/browser - Webpack (minified) 65.75 KB (0%)
@sentry/react - Webpack (gzipped + minified) 20.14 KB (0%)
@sentry/nextjs Client - Webpack (gzipped + minified) 46.91 KB (+2.07% 🔺)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.42 KB (-0.01% 🔽)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 24.85 KB (+0.02% 🔺)
@sentry/replay index.js 37.08 KB (0%)
@sentry/replay - Webpack (gzipped + minified) 37.05 KB (0%)
@sentry/replay - Webpack (minified) 124.54 KB (0%)

Base automatically changed from kmclb-nextjs-make-prefix-loader-generic to master December 2, 2022 09:44
@lforst lforst self-requested a review December 2, 2022 09:49
Copy link
Contributor

@lforst lforst left a comment

Choose a reason for hiding this comment

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

Nice investigative work on how the stack traces come together!

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-asssetPrefix-option branch from 668bf59 to 0f1f159 Compare December 2, 2022 10:27
@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-support-asssetPrefix-option branch from 0f1f159 to 720a75f Compare December 2, 2022 11:22
@lobsterkatie lobsterkatie merged commit 3f5c9c4 into master Dec 2, 2022
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-support-asssetPrefix-option branch December 2, 2022 14:23
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.

[nextjs] support assetPrefix option
2 participants