Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next 13: Usage of usePathname in client component results in SSR route instead of SSG #42416

Closed
1 task done
sdorra opened this issue Nov 3, 2022 · 2 comments
Closed
1 task done
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.

Comments

@sdorra
Copy link

sdorra commented Nov 3, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
Binaries:
  Node: 16.18.0
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: 7.14.2
Relevant packages:
  next: 13.0.1
  eslint-config-next: 13.0.1
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Usage of a client component which uses the usePathname hook results in SSR pages on build.

Client Component:

"use client";

import clsx from "clsx";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { FC, PropsWithChildren } from "react";

type Props = PropsWithChildren & {
  href: string;
  exact?: boolean;
  className?: string;
};

const NavLink: FC<Props> = ({ href, className, children, exact = true }) => {
  const path = usePathname();
  const isActive = exact ? href === path : path.startsWith(href);
  return (
    <Link
      className={clsx(className, {
        "text-stone-100 underline decoration-2 underline-offset-2 decoration-pink-600": isActive,
      })}
      href={href}
    >
      {children}
    </Link>
  );
};

export default NavLink;

If this component is used, the routes are rendered as SSR:

Route (app)                                Size     First Load JS
 λ /                                      0 B                0 B
 λ /about                                 141 B          64.5 kB
 λ /characters                            141 B          64.5 kB

If i replace the usage of the hook e.g.:

-  const path = usePathname();
+  const path = "";

The build renders the routes as SSG:

Route (app)                                Size     First Load JS
┌ ○ /                                      0 B                0 B
├ ○ /about                                 141 B          64.5 kB
├ ○ /characters                            141 B          64.5 kB

Expected Behavior

Should render the routes as SSG.

Link to reproduction

https://github.com/sdorra/rick-and-morty

To Reproduce

Run pnpm install and pnpm build, this should result in SSR routes. Removing the usage usePathname in the NavLink component, should result in SSG routes (after another pnpm build).

@sdorra sdorra added the bug Issue was opened via the bug report template. label Nov 3, 2022
@sdorra sdorra changed the title Next 13: Usage of usePathname in client component result in SSR instead of SSG on build Next 13: Usage of usePathname in client component results in SSR route instead of SSG Nov 3, 2022
@sdorra
Copy link
Author

sdorra commented Nov 4, 2022

Seems to be fixed with version 13.0.2 (#42440).

@sdorra sdorra closed this as completed Nov 4, 2022
@leerob leerob added the area: app App directory (appDir: true) label Nov 6, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Dec 6, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants