sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

@sentry/sveltekit import causing error with upgrade to 9.29.0

Open RikuVan opened this issue 6 months ago • 2 comments

Is there an existing issue for this?

  • [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [x] I have reviewed the documentation https://docs.sentry.io/
  • [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

9.29.0

Framework Version

sveltekit 2.21.1

Link to Sentry event

https://danceup-academy.sentry.io/issues/46616164/?alert_rule_id=183785&alert_timestamp=1749978555497&alert_type=email&environment=production&notification_uuid=d02d3778-4423-4f55-bd30-4154d0d3c61d&project=4509219253256272&referrer=alert_email

Reproduction Example/SDK Setup

import * as Sentry from '@sentry/sveltekit'
import type { Handle } from '@sveltejs/kit'
import { sequence } from '@sveltejs/kit/hooks'
import { paraglideMiddleware } from '$lib/paraglide/server'
import { getAccessToken } from '$lib/auth/getAccessToken'
import { env } from '$env/dynamic/private'
import { isRegistrationComplete } from '$lib/validation/isRegistrationComplete'

Sentry.init({
	dsn: 'https://foobar',
	tracesSampleRate: 1
})

const handleParaglide: Handle = ({ event, resolve }) =>
	paraglideMiddleware(event.request, ({ request, locale }) => {
		event.request = request

		return resolve(event, {
			transformPageChunk: ({ html }) => html.replace('%lang%', locale)
		})
	})

const handleSession: Handle = async ({ event, resolve }) => {
	const accessToken = await getAccessToken(event.cookies)

	if (!accessToken) {
		event.locals.user = null
		return resolve(event)
	}

	if (accessToken && !event.locals.user) {
		const res = await fetch(`${env.ROOT_API}/auth/me`, {
			headers: {
				Authorization: `Bearer ${accessToken}`
			}
		})

		if (res.ok) {
			const user = await res.json()
			event.locals.user = {
				...user,
				registrationIsComplete: isRegistrationComplete(user)
			}
		} else {
			console.error('Error fetching user in hooks.server.ts', res)
		}
	}

	event.locals.getAccessToken = getAccessToken

	return resolve(event)
}

export const handle: Handle = sequence(Sentry.sentryHandle(), handleSession, handleParaglide)

export const handleError = Sentry.handleErrorWithSentry()

Steps to Reproduce

  1. upgrade to ^9.29.0
  2. add code below to server.hooks.ts as seen in example
  3. get error causing app to crash in production

Expected Result

App renders without 500 error on backend

Actual Result

2025-06-15 11:36:27.285 | This file is being treated as an ES module because it has a '.js' file extension and '/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. |  

RikuVan avatar Jun 15 '25 09:06 RikuVan

Hi @RikuVan, thanks for writing in.

I tried reproducing this with paraglide and our 9.29.0 SDK but didn't run into issues. I did mock out getAccessToken but to be honest, I'm not super familiar with SvelteKit so this might be just something on my side.

Could you please provide a reproduction repo?

andreiborza avatar Jun 16 '25 11:06 andreiborza

Hi @RikuVan, thanks for writing in.

I tried reproducing this with paraglide and our 9.29.0 SDK but didn't run into issues. I did mock out getAccessToken but to be honest, I'm not super familiar with SvelteKit so this might be just something on my side.

Could you please provide a reproduction repo?

For me this only happens on a production build. Running the build using node build does cause this issue for me when opening a route (but not all of them).

It's probably related to these issues: https://github.com/sveltejs/kit/issues/13869 https://github.com/getsentry/sentry-javascript/issues/16507 https://github.com/sveltejs/kit/issues/13888

Personally I reverted back to (might be overkill)

[email protected]
@sveltejs/[email protected]
@sentry/[email protected] 

rbozan avatar Jun 16 '25 12:06 rbozan

Thanks for adding context. We'll look into it, but @Lms24 is ooo and it might take a bit to get resolved. If this is blocking, we recommend pinning to 9.27.0 for the time being.

andreiborza avatar Jun 17 '25 16:06 andreiborza

As @rbozan said this oddly seemed not to appear with the preview but only in production when opening the app, so I was a bit confused with making a repro and don't presently have time to dig in more. For my part I just removed Sentry from the server for now.

RikuVan avatar Jun 17 '25 17:06 RikuVan

A very simple workaround in the meantime is to add @sentry/sveltekit to the ssr.external option in vite.config.ts to prevent Vite from bundling it in. https://github.com/sveltejs/kit/issues/13869#issuecomment-2958203909

teemingc avatar Jun 18 '25 08:06 teemingc

A fix for this was released in version 9.28.1, so this issue can be closed. Comment mentioning the fix: https://github.com/sveltejs/kit/issues/13869#issuecomment-2961878138

s1gr1d avatar Jul 30 '25 08:07 s1gr1d