Skip to content

feat(nuxt): Add server config to root folder #13583

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
Sep 9, 2024
Merged

Conversation

s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Sep 4, 2024

This is a draft PR as this approach leads to an error as hook.mjs is not included in the node_modules. This has been fixed upstream but was not yet released for nuxt.

Makes it possible to include a sentry.server.config.ts file in the root folder alongside sentry.client.config.ts. Currently, it has to be added in the public folder which is not 100% ideal.

@s1gr1d s1gr1d mentioned this pull request Sep 4, 2024
@s1gr1d s1gr1d added the Package: nuxt Issues related to the Sentry Nuxt SDK label Sep 4, 2024
Copy link
Contributor

github-actions bot commented Sep 4, 2024

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 22.52 KB - -
@sentry/browser - with treeshaking flags 21.3 KB - -
@sentry/browser (incl. Tracing) 34.78 KB - -
@sentry/browser (incl. Tracing, Replay) 71.23 KB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.67 KB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 75.57 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 88.3 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 90.14 KB - -
@sentry/browser (incl. metrics) 26.83 KB - -
@sentry/browser (incl. Feedback) 39.6 KB - -
@sentry/browser (incl. sendFeedback) 27.19 KB - -
@sentry/browser (incl. FeedbackAsync) 31.9 KB - -
@sentry/react 25.28 KB - -
@sentry/react (incl. Tracing) 37.74 KB - -
@sentry/vue 26.67 KB - -
@sentry/vue (incl. Tracing) 36.61 KB - -
@sentry/svelte 22.65 KB - -
CDN Bundle 23.77 KB - -
CDN Bundle (incl. Tracing) 36.49 KB - -
CDN Bundle (incl. Tracing, Replay) 70.91 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 76.22 KB - -
CDN Bundle - uncompressed 69.63 KB - -
CDN Bundle (incl. Tracing) - uncompressed 108.21 KB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 219.87 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 233.07 KB - -
@sentry/nextjs (client) 37.51 KB - -
@sentry/sveltekit (client) 35.35 KB - -
@sentry/node 126.36 KB +1.11% +1.38 KB 🔺
@sentry/node - without tracing 98.84 KB +3.5% +3.34 KB 🔺
@sentry/aws-serverless 109.1 KB +1.48% +1.58 KB 🔺

View base workflow run

@s1gr1d s1gr1d force-pushed the sig/server-config-nuxt branch from 6cd63cb to ea95e37 Compare September 5, 2024 11:42
@nandi95
Copy link

nandi95 commented Sep 6, 2024

This indeed would be helpful as I have a project which a merge of a laravel a nuxt project. Nuxt project is in the folder nuxt and I have the srcDir in the nuxt config set. This module should respect that and the rootDir configuration of nuxt.

? path.join(cwd, 'public', `instrument.${type}.${e}`)

This indeed

@s1gr1d s1gr1d marked this pull request as ready for review September 6, 2024 14:12
@s1gr1d
Copy link
Member Author

s1gr1d commented Sep 6, 2024

@nandi95 If I understand you correctly, you are defining a curstom rootDir in the nuxt config?

@s1gr1d s1gr1d requested a review from Lms24 September 6, 2024 14:14
@nandi95
Copy link

nandi95 commented Sep 6, 2024

I only meant to say that I define a srcDir so it's not the default expected nuxt directory structure.
And remind that there's also a rootDir option.
This will especially be importent when nuxt 4 comes out and has a different folder structure.

if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.log(
'[Sentry] Using your `sentry.server.config` file for the server-side Sentry configuration. In case you have a `public/instrument.server` file, it will be ignored.',
Copy link
Member

Choose a reason for hiding this comment

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

(...) it will be ignored

This reads a bit confusing, which of the two files are being ignored?

I think we could check here for the existence of the file and log explicitly if that file is getting ignored, wdyt?

Also, we are ignoring the public file here but users will still potentially --import it. This log makes it sound like it's completely ignored.

Copy link
Member

@Lms24 Lms24 Sep 9, 2024

Choose a reason for hiding this comment

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

I think this log should be split up.

1st part (public): can we log a warning if we detect the file in public instead of the first part of this message? Otherwise, I don't think we need to bother users with it, right?

To the second part: Can we instead print the exact import command? Something like

"Make sure to add --import ./.output/sentry.server.config.mjs to your NODE_OPTIONS env variable"

if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.log(
'[Sentry] Successfully added the content of the `sentry.server.config` file as `instrument-sentry.mjs` to the `.output/server` directory',
Copy link
Member

Choose a reason for hiding this comment

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

l: Maybe we can use destination here?

@s1gr1d s1gr1d requested a review from andreiborza September 9, 2024 07:42
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Nice work! Other than the console log, this looks good to me!

We can follow up with the e2e tests after this is merged, given we hit some road blocks there

Comment on lines 49 to 65
it('should return the server file with .ts extension if it exists', () => {
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => {
return !(filePath instanceof URL) && filePath.includes('sentry.server.config.ts');
});

const result = findDefaultSdkInitFile('server');
expect(result).toBe('sentry.server.config.ts');
});

it('should return the client file with .mjs extension if it exists', () => {
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => {
return !(filePath instanceof URL) && filePath.includes('sentry.client.config.mjs');
});

const result = findDefaultSdkInitFile('client');
expect(result).toBe('sentry.client.config.mjs');
});
Copy link
Member

Choose a reason for hiding this comment

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

l: For some test extra credit (feel free to disregard) - we could use an it.each to test against all file types we search for.

@s1gr1d s1gr1d merged commit 45156d2 into develop Sep 9, 2024
102 checks passed
@s1gr1d s1gr1d deleted the sig/server-config-nuxt branch September 9, 2024 10:18
@wanderowski
Copy link

Hello! Thank you for the work you have done!

Could you please clarify, should we somehow modify the command for running the project on server in package.json? I see that now we can put sentry.server.config.ts in the root folder, but did not find any instructions on how to modify starting command. Previously, it was required to paste this:

{
  "scripts": {
    "preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview",
    "start": "node --import .output/public/instrument.server.mjs .output/server/index.mjs"
  }
}

If i put my config file in the root folder and deleted instrument.server.mjs from public folder, should I modify these commands as well?

@s1gr1d
Copy link
Member Author

s1gr1d commented Sep 10, 2024

@wanderowski This PR is not released yet and more elaborate docs on this are still coming for the official docs page.

The file can be added like this:

"start": "node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs",

As it only affects the server, it can only be added to the start script. The preview script attempts to load data from the .nuxt folder as well and this would not work. It worked with the public folder as this folder is the same during development and in production.

However, it could be that your environment variables from the .env file are not yet loaded. You can explicitly include them with --env-file=.env (like described here).

@wanderowski
Copy link

@s1gr1d Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nuxt Issues related to the Sentry Nuxt SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants