vercel / analytics

Privacy-friendly, real-time traffic insights
https://vercel.com/analytics
Mozilla Public License 2.0
422 stars 26 forks source link

TypeError: Cannot read properties of undefined (reading 'Analytics') with NextJS 13.0.0 #63

Closed JamesDHW closed 1 year ago

JamesDHW commented 1 year ago

Problem

When following the Quickstart instructions for Vercel Audiences, building my NextJS (13.0.0) project results in an error when running next build:

> next build

warn  - You have enabled experimental feature (appDir) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback

info  - Creating an optimized production build
info  - Compiled successfully
info  - Linting and checking validity of types
info  - Collecting page data
[    ] info  - Generating static pages (0/3)

TypeError: Cannot read properties of undefined (reading 'Analytics')
    at Oa (.next/server/chunks/759.js:1025:56)
    at Ia (.next/server/chunks/759.js:1138:50)
    at Array.toJSON (.next/server/chunks/759.js:915:32)
    at stringify (<anonymous>)
    at S (./.next/server/chunks/759.js:1213:51)
    at Object.start (.next/server/chunks/759.js:1324:53)
    at setupReadableByteStreamController (node:internal/webstreams/readablestream:2665:23)
    at setupReadableByteStreamControllerFromSource (node:internal/webstreams/readablestream:2702:3)
    at new ReadableStream (node:internal/webstreams/readablestream:240:7)
    at exports.renderToReadableStream (.next/server/chunks/759.js:1321:24)

[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] {
  digest: '3412299338'
}

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of undefined (reading 'Analytics')

Minimum reproducible example

I can reproduce this by:

  1. Running pnpm create next-app
  2. Setting the NextJS version to 13.0.0 in the package.json a. pnpm install again to install 13.0.0 b. remove the exported metadata constant from layout.tsx which wasn't supported in NextJS 13.0.0 c. remove the import { Inter } from 'next/font/google' from page.tsx
  3. Follow the Quickstart Guide for NextJS 13 with the Analytics tag in the /app directory.
  4. Run pnpm run build which should fail with the above error!

The fix

I updated my NextJS version to "next": "13.2.4",!

I couldn't find any other issues relating to this or mention in the docs (<Analytics /> only working for given versions of NextJS) - hopefully posting the issue helps!