Next.js v13 introduced the new routing model, App Router, which supports server side components and requires a new project structure. This epic is to track the router migration process from page to app.
This doc will be used to create Epic/file issues for the migration in future sprints. As this is an ongoing work in progress, specifics may change based on the 1:1 chat.
[!note]
Before upgrading to version 14 , we'll use version 13 to migrate from the page router to the app router as it allows incremental router updates. Upgrading from 13 to 14 will be easier and faster once it's completed.
Package Replacement (blocker)
To prevent the Module not found: Can't resolve 'unfetch' error, replace isomorphic-fetch(not actively maintained) with cross-fetch.
During the migration, keep _app and _document to prevent the pages/* routes from breaking. Once fully migrated, they can be safely deleted.
Some hooks and their props need to be removed, replaced, and/or re-imported. And these new hooks are only supported in client components and cannot be used in server components.
Dynamic routes, context updates for the API, and data fetching are handled in the Data Fetching section below.
At each step, the numbered items (e.g., ❶, ❷, ❷) represent tickets.
Step 1: Add Top-level Files for App Router
In this step, we'll create the top-level routing files for the app router which are sufficient to build routing files for static pages.
❶ Create app directory and the root layout file
The root layout is a replacement for the _app.js and _document.js files.
❸ Add the globally shared UI components to the root layout
We'll refactor the globally shared UI components and add them to the root layout, and migrate routing hooks as needed.
[!note]
We'll file a corresponding issue for each component
Components marked "(later)" are excluded in this step
Refactor the globally shared components below:
Layout
Band (refactor sub-components as necessary)
ParticlesBg
Header (refactor sub-components as necessary)
GlobalNav
GithubAnchor
LogoAnchor
NavDropDown
NavIcon
NavLink
Footer
Notification(later)
ErrorPage(later)
PageTitle(later)
Re-import the useRouter hook from next/navigation and clean up the removed router properties (e.g., use the usePathname hook to return pathname, clean up asPath and add a workaround logic for it).
Revise the shared contexts below in the corresponding issues:
BandContextProvider
DatasetManagerContextProvider(later)
ModalContextProvider(later)
RefinebioContextProvider(later)
SearchManagerContextProvider(later)
(The context provider cannot be used in server components and must be handled using client components.)
❹ Add Custom 404 Error in app
Create a not-found.js file for the whole application in app.
Import shared/Error/Custom404 into the file.
We may also define a route segment error file per route which will be handled later as needed (e.g., API data fetching per endpoints). See Error Handling.
In this step, we'll create the routing files for static pages and migrate routing hooks as needed.
❶ Create routes for the legal pages
Create the route group for the following pages:
License
Privacy
Terms
Move components/shared/PageStatic to this route group folder and convert it to a route group layout (e.g., StaticPageLayout)
❷ Create route for About page
Create the app/about folder.
Define page.js in this folder.
❸ Create route for Home page
Create the app/home folder.
Define page.js in app folder.
Page Meta Tags (in progress - this section still needs to be defined)
Currently meta tags are added to pages using components/shared/PageTitle. However, next/head has been replaced by the new built-in SEO Metadata API, and we need to adjust the way we handle metadata in each route.
For this change, we'll file a few issues.
Data Fetching (in progress - this section still needs to be defined)
--- in progress: editing ---
Context
Next.js v13 introduced the new routing model, App Router, which supports server side components and requires a new project structure. This epic is to track the router migration process from
page
toapp
.The issues tracked by this epic as follows:
Problem or idea
This doc will be used to create Epic/file issues for the migration in future sprints. As this is an ongoing work in progress, specifics may change based on the 1:1 chat.
The list of potential issues can be found at https://github.com/AlexsLemonade/refinebio-web/issues/339#issuecomment-2120703500 which will be continuously adjusted/updated as needed.
Package Replacement (blocker)
To prevent the
Module not found: Can't resolve 'unfetch'
error, replaceisomorphic-fetch
(not actively maintained) withcross-fetch
.Config File
We need to migrate the ESLint config as follows:
Upgrade Eslint version by running
yarn add -D eslint-config-next@latest
Update
eslintrc.js
eslint-config-next
by runningyarn add -D @next/eslint-plugin-next
.eslint-config-next
toextends
property.Run
yarn lint
and fix newly generated lining warnings/errorsShared Features
Next.js 13 introduced a new change to the built-in Link component and we need to:
Anchor
(src/components/shared/Anchor.js
) component to use the new<Link>
component feature.App Router
Resource: Project Structure & File Conventions, App Router Incremental Adoption Guide
Step 1: Add Top-level Files for App Router
In this step, we'll create the top-level routing files for the app router which are sufficient to build routing files for static pages.
❶ Create
app
directory and the root layout fileThe root layout is a replacement for the
_app.js
and_document.js
files.src/app
folder.app/layout.js
file.❷ Add Google Fonts and Global Style to the root layout
❸ Add the globally shared UI components to the root layout
We'll refactor the globally shared UI components and add them to the root layout, and migrate routing hooks as needed.
Layout
Band
(refactor sub-components as necessary)ParticlesBg
Header
(refactor sub-components as necessary)GlobalNav
GithubAnchor
LogoAnchor
NavDropDown
NavIcon
NavLink
Footer
Notification
(later)ErrorPage
(later)PageTitle
(later)Re-import the
useRouter
hook fromnext/navigation
and clean up the removedrouter
properties (e.g., use theusePathname
hook to returnpathname
, clean upasPath
and add a workaround logic for it).Revise the shared contexts below in the corresponding issues:
BandContextProvider
DatasetManagerContextProvider
(later)ModalContextProvider
(later)RefinebioContextProvider
(later)SearchManagerContextProvider
(later)(The context provider cannot be used in server components and must be handled using client components.)
❹ Add Custom 404 Error in
app
not-found.js
file for the whole application inapp
.shared/Error/Custom404
into the file.We may also define a route segment error file per route which will be handled later as needed (e.g., API data fetching per endpoints). See Error Handling.
Step 2: Create Static Routes for Static Pages
Resource: Migrating Pages, Route Group
In this step, we'll create the routing files for static pages and migrate routing hooks as needed.
❶ Create routes for the legal pages
components/shared/PageStatic
to this route group folder and convert it to a route group layout (e.g.,StaticPageLayout
)❷ Create route for
About
pageapp/about
folder.page.js
in this folder.❸ Create route for
Home
pageapp/home
folder.page.js
inapp
folder.Page Meta Tags (in progress - this section still needs to be defined)
Currently meta tags are added to pages using
components/shared/PageTitle
. However,next/head
has been replaced by the new built-in SEO Metadata API, and we need to adjust the way we handle metadata in each route.For this change, we'll file a few issues.
Data Fetching (in progress - this section still needs to be defined)
Resource: Migrating Data Fetching Methods, Data Fetching
Project Reorganization (in progress - this section still needs to be defined)
Colocation, Project Organization Features
e.g. ) Move the sub-components in
components/About/*
to this folder for colocation.Solution or next step