• Stars
    star
    132
  • Rank 274,205 (Top 6 %)
  • Language
    TypeScript
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A demonstration of how to use React Router inside Next.js

Building a single page application with Next.js and React Router

There are many reasons to use React Router inside a Next.js project! React Router is far more flexible than Next's router and makes it easy to share layout and state between among routes, even deeply nested ones. Doing this with Next.js requires consolidating all your shared logic in a custom _app.tsx component and using complicated layout hacks.

If you're building a single-page application and SEO isn't a concern, using React Router with Next.js is a powerful combination. Unfortunately there is no guidance for how to do this provided by th Next.js team. This repo demonstrates how this can be achieved.

For the full description of this project, go to https://colinhacks.com/essays/building-a-spa-with-nextjs.

The approach

The basic idea:

  1. Create a custom App (/pages/_app.tsx)

  2. Return null if typeof window === "undefined". This is required to prevent react-router from throwing errors during the SSR step!

// pages/_app.tsx

import { AppProps } from 'next/app';

function App({ Component, pageProps }: AppProps) {
  return (
    <div suppressHydrationWarning> // <- ADD THIS
      {typeof window === 'undefined' ? null : <Component {...pageProps} />}
    </div>
  );
}

export default App;
  1. Rewrite all routes to the homepage
// next.config.js

module.exports = {
  async rewrites() {
    return [
      // Rewrite everything else to use `pages/index`
      {
        source: '/:path*',
        destination: '/',
      },
    ];
  },
};

Go to https://colinhacks.com/essays/building-a-spa-with-nextjs for more details.

Feel free to tweet questions to me @colinhacks πŸ€™

More Repositories

1

zod

TypeScript-first schema validation with static type inference
TypeScript
33,028
star
2

devii

A developer blog starter for 2021 (Next.js + React + TypeScript + Markdown + syntax highlighting)
TypeScript
496
star
3

next-firebase-ssr

An Next.js example repo for building authenticated pages with Firebase Authentication, cookies, and getServerSideProps
TypeScript
419
star
4

tozod

TypeScript
140
star
5

bye-react

Command-line tool to migrate React to Preact
JavaScript
126
star
6

typejest

Test your types
TypeScript
26
star
7

normi

A zero-config normalized cache for MobX
TypeScript
20
star
8

zod-deno

TypeScript-first schema validation for Deno
TypeScript
16
star
9

trailhead

Find paths through your data in a type-safe way
TypeScript
12
star
10

nxtk

A TypeScript toolkit for Next.js
TypeScript
12
star
11

skee

Fluent schema modeling and migrations
TypeScript
8
star
12

npm-ts-starter

A project template for TypeScript npm packages
TypeScript
8
star
13

bun-workspaces

TypeScript
4
star
14

gwen

Utility-first CSS-in-JS styling library for maniacs
TypeScript
4
star
15

typesafe-api-no-graphql

A typesafe API β€” no GraphQL required
TypeScript
4
star
16

zod-locales

Localized error maps for Zod
3
star
17

DougNotes

Graphical dynamic knowledge repository platform
JavaScript
3
star
18

edgedb-movies

TypeScript
2
star
19

edgedb-nextjs-blog

TypeScript
2
star
20

live-typescript-monorepo

Strategies for live-updating TypeScript types in monorepos
TypeScript
1
star
21

edgedb-remix-test-2

JavaScript
1
star
22

kickdata

A scraper deployed to the Google App Engine that scrapes all soon-to-end Kickstarter projects every 10 minutes and stores relevant data in a Parse database.
Python
1
star
23

pensiv

Realtime Thoughtstreaming app with Meteor and Ionic
JavaScript
1
star
24

colinhacks

1
star
25

onepotpony

TypeScript
1
star
26

repros

TypeScript
1
star