You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
False positive pushstate warnings with SvelteKit #13823
Just start a sveltekit app with sentry and on first page load you will see the warning.
seems like initiating here // node_modules/@sentry-internal/browser-utils/build/esm/instrument/history.js
[Warning] Avoid using history.pushState(...)andhistory.replaceState(...)as these will conflict with SvelteKit's router. Use thepushStateandreplaceStateimports from$app/navigation instead. (client.js, line xxx)
Expected Result
No wanning while using svelte app navigation.
Actual Result
[Warning] Avoid using history.pushState(...)andhistory.replaceState(...)as these will conflict with SvelteKit's router. Use thepushStateandreplaceStateimports from$app/navigation instead. (client.js, line xxx)
Possible fix: // node_modules/@sentry-internal/browser-utils/build/esm/instrument/history.js
import{addHandler,maybeInstrument,supportsHistory,triggerHandlers,fill}from'@sentry/utils';import{WINDOW}from'../types.js';letlastHref;// Check if SvelteKit is in the environmentconstisSvelteKit=(()=>{try{returntypeofwindow!=='undefined'&&window.__sveltekit_dev!==undefined;}catch{returnfalse;}})();/** * Add an instrumentation handler for when a fetch request happens. * The handler function is called once when the request starts and once when it ends, * which can be identified by checking if it has an `endTimestamp`. * * Use at your own risk, this might break without changelog notice, only used internally. * @hidden */functionaddHistoryInstrumentationHandler(handler){consttype='history';addHandler(type,handler);maybeInstrument(type,instrumentHistory);}functioninstrumentHistory(){if(!supportsHistory()){return;}constoldOnPopState=WINDOW.onpopstate;WINDOW.onpopstate=function( ...args){constto=WINDOW.location.href;// keep track of the current URL state, as we always receive only the updated stateconstfrom=lastHref;lastHref=to;consthandlerData={ from, to };triggerHandlers('history',handlerData);if(oldOnPopState){// Apparently this can throw in Firefox when incorrectly implemented plugin is installed.// https://github.com/getsentry/sentry-javascript/issues/3344// https://github.com/bugsnag/bugsnag-js/issues/469try{returnoldOnPopState.apply(this,args);}catch(_oO){// no-empty}}};functionhistoryReplacementFunction(originalHistoryFunction){returnfunction( ...args){consturl=args.length>2 ? args[2] : undefined;if(url){// coerce to string (this is what pushState does)constfrom=lastHref;constto=String(url);// keep track of the current URL state, as we always receive only the updated statelastHref=to;consthandlerData={ from, to };triggerHandlers('history',handlerData);}// Use SvelteKit navigation if in SvelteKit environmentif(isSvelteKit){try{if(originalHistoryFunction.name==='pushState'){import('$app/navigation').then(({ pushState })=>pushState(url)).catch(err=>console.error('Failed to push state:',err));return;// Ensure to exit after calling pushState}elseif(originalHistoryFunction.name==='replaceState'){import('$app/navigation').then(({ replaceState })=>replaceState(url)).catch(err=>console.error('Failed to replace state:',err));return;// Ensure to exit after calling replaceState}}catch(err){console.error(`Failed to import SvelteKit navigation for ${originalHistoryFunction.name}:`,err);}}// Fallback to native behaviorreturnoriginalHistoryFunction.apply(this,args);};}fill(WINDOW.history,'pushState',historyReplacementFunction);fill(WINDOW.history,'replaceState',historyReplacementFunction);}export{addHistoryInstrumentationHandler};//# sourceMappingURL=history.js.map
Hi @AliTahir-101 thanks for writing in! I noticed this in a side project of mine also recently and wanted to follow up on it. I'll look into SvelteKit code to understand where this warning is triggered. Ideally, we find a way to work around this within the @sentry/sveltekit package. The suggested fix would increase bundle size for all browser SDKs so ideally we avoid that :)
Hi @AliTahir-101 thanks for writing in! I noticed this in a side project of mine also recently and wanted to follow up on it. I'll look into SvelteKit code to understand where this warning is triggered. Ideally, we find a way to work around this within the @sentry/sveltekit package. The suggested fix would increase bundle size for all browser SDKs so ideally we avoid that :)
Hey! My suggestion was meant to provide a quick workaround based on my observations, but I recognize that a more optimal solution would involve adjustments within the sentry/sveltekit package itself.
I’m currently focused on my project, so I haven’t had the time to dive deep into debugging the Sentry integration. I appreciate your efforts in finding a more suitable resolution, and I look forward to any updates you may have. Thank you!
Can you confirm for me that this is only a problem in dev mode? From reading the sveltekit code around it, I think it should be limited to dev but I'd like to make sure that's the case
Activity
AliTahir-101 commentedon Sep 27, 2024
Possible fix:
// node_modules/@sentry-internal/browser-utils/build/esm/instrument/history.jsLms24 commentedon Sep 27, 2024
Hi @AliTahir-101 thanks for writing in! I noticed this in a side project of mine also recently and wanted to follow up on it. I'll look into SvelteKit code to understand where this warning is triggered. Ideally, we find a way to work around this within the
@sentry/sveltekitpackage. The suggested fix would increase bundle size for all browser SDKs so ideally we avoid that :)AliTahir-101 commentedon Sep 27, 2024
Hey! My suggestion was meant to provide a quick workaround based on my observations, but I recognize that a more optimal solution would involve adjustments within the sentry/sveltekit package itself.
I’m currently focused on my project, so I haven’t had the time to dive deep into debugging the Sentry integration. I appreciate your efforts in finding a more suitable resolution, and I look forward to any updates you may have. Thank you!
Lms24 commentedon Sep 27, 2024
Can you confirm for me that this is only a problem in dev mode? From reading the sveltekit code around it, I think it should be limited to dev but I'd like to make sure that's the case
20 remaining items