Howdy, I'm trying to sort out what the recommended...
# support
p
Howdy, I'm trying to sort out what the recommended integration path is in
Next.js 13 (with App Router)
... We currently load Rudderstack via npm (v2), and due to some Server-Side-Rendering (SSR) quirks, we continuously ran into a
window is not defined
error on the server. This doesn't prevent Rudderstack from loading properly, but has created an obscene amount of noise in our logs and reduced the effectiveness of some of our functional/e2e tests. In searching for a solution, I discovered the
V3
release, and was pleased to see that things have improved and that there is even mention of Next.js in the docs--however, it only mentions it for the
npm
integration path. So my questions are: • When importing from
@rudderstack/analytics-js/bundled
as the docs recommend for Next.js, we lose our type definitions
"Cannot find module '@rudderstack/analytics-js/bundled' or its corresponding type declarations"
--how can we fix this? • Is the
npm
integration the recommended path for Next.js's new App Router, or could the CDN snippet also work? • We only create new
RudderAnalytics
instances and call
load()
from inside of
useEffect()
hooks, so we're confused why we're getting
window is not defined
on the server unless the Rudderstack npm package does some self-initialization immediately upon
import
, causing some of its code to be ran when Next.js makes an initial render of client-side components on its server before hydrating on the client. Can you provide any insight to this behavior?
m
Hey There! 👋 Your message has been received by the RudderStack team. Our standard customer support hours are 9-6 PM EST, but we will forward this request to your Technical Account Manager, and they will get back to you shortly. Please use the thread for any additional comments.
p
Also, when attempting to integrate with with
v3
via
npm
in Next.js, I'm seeing lots of Hydration mismatch errors in the browser:
Copy code
react-dom.development.js:6824 Uncaught Error: Hydration failed because the initial UI does not match what was rendered on the server.
    at throwOnHydrationMismatch (webpack-internal:///(:3000/app-pages-browse
m
Hi James I am using the NPM package of the v3 version with my react JS app. Can you use the following code to import Rudderstack
Copy code
import { RudderAnalytics } from '@rudderstack/analytics-js/bundled';
However, the device mode integrations do not still throw some errors with the v3 SDK.
p
I am using that line of code to import the SDK
regardless, I still get the Hydration error
@shy-kite-21035 have ya'll tested out the new SDK in apps that server-side-render?
m
The team did test with a NextJS app but the docs have not been updated for using the NextJS app. Let me check with the team on how to fix the above error for a NextJS app.
p
@shy-kite-21035 thank you
to be clear, I'm using the latest Next.js version with their new App Router, and calling
rudderanalytics.page()
from within a
useEffect
of a client component for each page.
It seems to me that the error is originating from the Rudderstack injecting script tags into the DOM outside of the React Render Lifecycle--why is it doing this?
I see that it's doing it for some plugins--including Bugsnag--but the error occurs even when I'm not using Bugsnag
m
Oh ok. I will share an update on this in a day or two.
Sorry this is taking longer than expected. Can you give me a couple of more days.
Hi James the team mentioned that this error is not related to the RudderStack JS SDK. Can you recheck?