Podcast
Questions and Answers
How can you enable TypeScript checks for your Next.js routes?
How can you enable TypeScript checks for your Next.js routes?
- By installing the `@types/next` package.
- By adding a `.ts` extension to your route files.
- By running the `next typecheck` command.
- By setting `typedRoutes: true` in the `experimental` section of `next.config.js`. (correct)
Which of the following is an official Next.js package for integrating Google services?
Which of the following is an official Next.js package for integrating Google services?
- `@next/google-analytics`
- `next-google-tag-manager`
- `@next/third-parties/google` (correct)
- `@next/google-maps`
According to Next.js's recommendation, which .env
file(s) should be committed to your repository?
According to Next.js's recommendation, which .env
file(s) should be committed to your repository?
- None of the `.env` files
- Only `.env.example`
- `.env`, `.env.development`, and `.env.production` (correct)
- `.env` and `.env.local`
How can you dynamically generate a sitemap.xml
in Next.js?
How can you dynamically generate a sitemap.xml
in Next.js?
What is the purpose of unstable_cache
in the Next.js app router?
What is the purpose of unstable_cache
in the Next.js app router?
How can you configure your CI to leverage the Next.js build cache?
How can you configure your CI to leverage the Next.js build cache?
When deploying on Vercel, how can you access the geographic location of a user?
When deploying on Vercel, how can you access the geographic location of a user?
How can you automatically remove JSX properties like data-testid
in a Next.js application?
How can you automatically remove JSX properties like data-testid
in a Next.js application?
What is the purpose of next/dynamic
in Next.js?
What is the purpose of next/dynamic
in Next.js?
Under what circumstances will Fast Refresh perform a full reload in Next.js?
Under what circumstances will Fast Refresh perform a full reload in Next.js?
What is the purpose of instrumentation.ts
in a Next.js project?
What is the purpose of instrumentation.ts
in a Next.js project?
How can you enable type checking in your next.config.js
file?
How can you enable type checking in your next.config.js
file?
What is the purpose of waitUntil
in Next.js middleware?
What is the purpose of waitUntil
in Next.js middleware?
How can you include multi-line values in your .env
file?
How can you include multi-line values in your .env
file?
In the Next.js App Router, what is the purpose of exporting dynamic = 'force-dynamic'
in a route segment?
In the Next.js App Router, what is the purpose of exporting dynamic = 'force-dynamic'
in a route segment?
How can you set the theme-color
meta tag in a Next.js application using the App Router?
How can you set the theme-color
meta tag in a Next.js application using the App Router?
How can you determine if a request is coming from a known bot in Next.js middleware?
How can you determine if a request is coming from a known bot in Next.js middleware?
How can you conditionally modify the Next.js configuration based on the environment (e.g., development vs. production)?
How can you conditionally modify the Next.js configuration based on the environment (e.g., development vs. production)?
How can you enable debug mode when building a Next.js application?
How can you enable debug mode when building a Next.js application?
In the App Router, which file should you use to catch errors thrown in the root app/layout.js
component?
In the App Router, which file should you use to catch errors thrown in the root app/layout.js
component?
In the App Router, what is the recommended way to set meta tags and the title tag for a page?
In the App Router, what is the recommended way to set meta tags and the title tag for a page?
What is the purpose of the replace
prop on the Next.js <Link>
component?
What is the purpose of the replace
prop on the Next.js <Link>
component?
What does the scroll
prop on the Next.js <Link>
component control?
What does the scroll
prop on the Next.js <Link>
component control?
How can you configure the target browsers for which Next.js will automatically polyfill JavaScript features?
How can you configure the target browsers for which Next.js will automatically polyfill JavaScript features?
What is the benefit of using a custom Next.js server?
What is the benefit of using a custom Next.js server?
What is the purpose of multi-zone setup in Next.js?
What is the purpose of multi-zone setup in Next.js?
How can you return a 404 error from getServerSideProps
in the Pages Router?
How can you return a 404 error from getServerSideProps
in the Pages Router?
How can you redirect the user to a different page from within getServerSideProps
(Pages Router)?
How can you redirect the user to a different page from within getServerSideProps
(Pages Router)?
Which configuration option improves the performance of a Next.js application by caching data fetched within React Server Components?
Which configuration option improves the performance of a Next.js application by caching data fetched within React Server Components?
You are using the App Router and need to set the robots
meta tag. Which function should you use in a server component to accomplish this?
You are using the App Router and need to set the robots
meta tag. Which function should you use in a server component to accomplish this?
What is the purpose of the maxDuration
export in a Next.js route segment when using the App Router?
What is the purpose of the maxDuration
export in a Next.js route segment when using the App Router?
How does setting dynamicParams = false
in a dynamic route segment affect the build process in the Next.js App Router?
How does setting dynamicParams = false
in a dynamic route segment affect the build process in the Next.js App Router?
If you have a file which renders a React component but also exports a value that is imported by a non-React component, what is the resolution to enable Fast Refresh to work?
If you have a file which renders a React component but also exports a value that is imported by a non-React component, what is the resolution to enable Fast Refresh to work?
You are using a third-party library that requires some initialization code to run when the Next.js server starts. Where would you place this initialization code using the App Router?
You are using a third-party library that requires some initialization code to run when the Next.js server starts. Where would you place this initialization code using the App Router?
You're building a Next.js application and want to prevent the automatic prefetching of a specific <Link>
. How would you achieve this objective?
You're building a Next.js application and want to prevent the automatic prefetching of a specific <Link>
. How would you achieve this objective?
Flashcards
typedRoutes
typedRoutes
Enable TypeScript checks for routes by setting typedRoutes: true
in next.config.js
. Generates a .d.ts
file with route typing information.
Next.js Google Third-Party Packages
Next.js Google Third-Party Packages
Official Next.js packages for integrating Google services like Google Tag Manager, Analytics, Maps, and YouTube embeds into your application.
.env in Repository
.env in Repository
Next.js recommends including .env
, .env.development
, and .env.production
in your repository to define defaults. Keep secrets in .env*.local
and add to .gitignore
.
sitemap.ts
sitemap.ts
Signup and view all the flashcards
unstable_cache
unstable_cache
Signup and view all the flashcards
CI Build Cache
CI Build Cache
Signup and view all the flashcards
Vercel Geolocation
Vercel Geolocation
Signup and view all the flashcards
Remove JSX Properties
Remove JSX Properties
Signup and view all the flashcards
Dynamic Import
Dynamic Import
Signup and view all the flashcards
Fast Refresh
Fast Refresh
Signup and view all the flashcards
instrumentation.ts
instrumentation.ts
Signup and view all the flashcards
Type Check next.config.js
Type Check next.config.js
Signup and view all the flashcards
Middleware waitUntil
Middleware waitUntil
Signup and view all the flashcards
.env File Variables
.env File Variables
Signup and view all the flashcards
Route Segment Config
Route Segment Config
Signup and view all the flashcards
generateViewport
generateViewport
Signup and view all the flashcards
User Agent Detection
User Agent Detection
Signup and view all the flashcards
next.config.js Phase
next.config.js Phase
Signup and view all the flashcards
Next.js Debugging
Next.js Debugging
Signup and view all the flashcards
Global Error Page
Global Error Page
Signup and view all the flashcards
Metadata API
Metadata API
Signup and view all the flashcards
Link replace Option
Link replace Option
Signup and view all the flashcards
Link scroll Option
Link scroll Option
Signup and view all the flashcards
Link prefetch Option
Link prefetch Option
Signup and view all the flashcards
Browserlist Config
Browserlist Config
Signup and view all the flashcards
Custom Next.js Server
Custom Next.js Server
Signup and view all the flashcards
Multi zones configuration
Multi zones configuration
Signup and view all the flashcards
404/Redirect in getServerSideProps
404/Redirect in getServerSideProps
Signup and view all the flashcards
Study Notes
- Guide covers lesser-known features of NextJS for developers familiar with both pages and app router.
Opt-in to Automatic Typescript Checks
- Enable
typedRoutes
innext.config.js
to generate a.d.ts
types file in.next/types
. - This allows Typescript to validate
href
values within your routes and prevent typos in components. - Non-literal strings, like dynamically created strings (e.g.,
/blog/${slug}
), require casting asRoute
.
Official NextJS Packages for Google Third-Party Scripts
@next/third-parties/google
package provides official components for Google services.GoogleTagManager
: Instantiates a Google Tag Manager container.GoogleAnalytics
: Sets up Google Analytics.GoogleMapsEmbed
: Lazy loads a Google Maps instance.YouTubeEmbed
: Loads a YouTube embed using thelite-youtube-embed
package.
Recommendation for Committing .env
to Repository
- NextJS documentation suggests including
.env
,.env.development
, and.env.production
in your repository to define defaults. .env*.local
files should be in.gitignore
for storing secrets.
Sitemap.xml Generation
- Create a
sitemap.ts
file in theapp
directory to generate an array of URLs for yoursitemap.xml
file. - The function can asynchronously load data from an API or database to dynamically populate the sitemap.
unstable_cache (App Router)
unstable_cache
can cache data, like database queries, for use across multiple requests.- Despite being unstable, it generally functions as expected, though its implementation may evolve in future NextJS versions.
Improve CI Build Cache
- Next.js uses the
.next/cache
directory to store cached data between builds. - Configure your CI provider to cache this directory for faster build times.
- Vercel handles this automatically.
Get User Geolocation (Vercel)
- Vercel automatically includes geolocation data in the
NextRequest
object. - Access user's city, country, region, latitude, and longitude through
request.geo.*
. - Get the user's IP address using
request.ip
on Vercel orrequest.headers.get('X-Forwarded-For')
on other servers.
Remove JSX Properties
babel-plugin-react-remove-properties
removes JSX properties likedata-testid
.- Configure the plugin in
next.config.js
to remove specific properties by regex.
Dynamic Loader
next/dynamic
combinesReact.lazy()
and for dynamic component loading.- It defers loading a component's code until it's first rendered and allows specifying a fallback component.
- Also loads other scripts/files on demand
Fast Refresh (Hot Reloading)
- "Fast refresh" is the near-instant reloading of components without losing state during file edits.
- Editing a file with React components updates only that file and re-renders the component.
- Editing a file with non-React component exports re-runs that file and files importing it.
- Editing a file imported by non-React files triggers a full reload.
- Use
// @refresh reset
to force a full reload.
instrumentation.ts
instrumentation.ts
in the root orsrc
directory executes aregister
function when the NextJS server starts.- Use it for initializing external code required for your application.
Type Check next.config.js
- Add
// @ts-check
at the top ofnext.config.js
to enable Typescript type checking in your IDE.
Middleware waitUntil and NextFetchEvent (App Router)
- In middleware, use
event.waitUntil
to run code in the background.
Multiline .env Variables
- Can have multiline value with escaped new lines.
Route Segment Configuration
- Exporting variables from
page.tsx
,layout.tsx
, orroute.ts
in the App Router configures the route. - Configurable values include
dynamic
,dynamicParams
,revalidate
,fetchCache
,runtime
,preferredRegion
, andmaxDuration
. - Values must be static.
generateViewport in Server Components (App Router)
- Use
generateViewport
in server components to set meta tags likethemeColor
.
User Agent Information with isBot
- Import
userAgent
fromnext/server
to get information about the user agent. isBot
boolean indicates if the request is from a known bot.- The device object provides device details (model, type, vendor).
next.config.js Phase
- Use the phase parameter in
next.config.js
to return different configurations for different contexts (development, server, build, testing.
Debugging Builds
- Run
next build --debug
to build in debug mode. - Run
next build --profile
to build in profile mode. next info
prints local platform information.
Global Error Page (App Router)
- Use
app/global-error.ts
to catch errors thrown in the rootapp/layout.js
orapp/template.js
component, wrapping the entire application. app/error.ts
does not catch errors thrown in the rootapp/layout.js
orapp/template.js
component.app/global-error.ts
requires including and `.
Metadata API (App Router)
- Use the Metadata API to set meta tags or title tag when using the app router.
- Use static metadata or dynamic metadata.
Boolean Options
- Has boolean props which change the behavior of the component
Replace Option on Link Component
- Set
replace={true}
on the component to replace the current history state instead of adding a new URL.
Scroll Option on Link Component
- The
scroll
boolean prop on prevents scrolling to the top of the page on navigation.
Prefetching
- The
prefetch
boolean on (enabled by default) prefetches the link's URL in the background.
Configure Browserlist for Polyfills
- Configure
browserslist
inpackage.json
to override default browser versions for polyfilling.
Custom Next.js Server
- Set up a custom Next.js server for complex application requirements.
- Integrate Next.js rendering logic into an Express app.
Multi Zones (Multiple Next.JS Application Deployments)
- Split a large app into separate apps with their own deployments using rewrites in
next.config.js
. - Set
basePath
in the child application'snext.config.js
.
Return 404 or Redirect from getServerSideProps() (Pages Router)
- Return
{ notFound: true }
fromgetServerSideProps
to return a 404. - Return a redirect object to force a redirect.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.