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

template.js not executing on every request #60032

Open
1 task done
ghost opened this issue Dec 29, 2023 · 5 comments
Open
1 task done

template.js not executing on every request #60032

ghost opened this issue Dec 29, 2023 · 5 comments
Labels
bug Issue was opened via the bug report template.

Comments

@ghost
Copy link

ghost commented Dec 29, 2023

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/cool-lamport-rnwm47

To Reproduce

Have a file structure that looks like this:
/app
│ layout.js
│ template.js
│ /profile
└─/website
│ │ /view
│ │ /edit

Current vs. Expected behavior

Current behavior:
When navigation between profile and website/view or profile and website/edit the console.log writes 'Template.jsx executed' every single time the route is changes

When navigation between website/view and website/edit the console.log writes 'Template.jsx executed' only the first time. if you continue to navigation between website/view and website/edit console.log will not write 'Template.jsx executed'

Expected behavior:
the console.log writes 'Template.jsx executed' every single time the route is changes
e.g. navigation between website/view and website/edit AND navigation between profile and website/view

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 14.0.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

No response

@ghost ghost added the bug Issue was opened via the bug report template. label Dec 29, 2023
@user-0a
Copy link

user-0a commented Jan 12, 2024

Also experiencing this issue.

@aramikuto
Copy link
Contributor

It seems like only a change in immediate child segment triggers template re-rendering. (For example, / to /profile or /profile to /settings). When navigating from /website/view to /website/edit, children segment remains the same (website).

I wonder if that's how it's supposed to work.

@ghost
Copy link

ghost commented Jan 14, 2024

It seems like only a change in immediate child segment triggers template re-rendering. (For example, / to /profile or /profile to /settings). When navigating from /website/view to /website/edit, children segment remains the same (website).

I wonder if that's how it's supposed to work.

@aramikuto
I would assume not since the docs specifically say

templates create a new instance for each of their children on navigation.

and it mentions logging page views which you cannot do if it is not executing on every navigation.

While less common, you might choose a template over a layout if you want:
Features that rely on useEffect (e.g logging page views) and useState (e.g a per-page feedback form).

Reference: File Conventions: template.js | Next.js

@andelkocvjetkovic
Copy link

andelkocvjetkovic commented Feb 2, 2024

I have same issue, I expected that the template is recreated on every navigation but it seems that is not a case for the nested routes.

@rwieruch
Copy link
Contributor

rwieruch commented Apr 17, 2024

Experiencing this problem as well. My expectation would have been the same: when there is a top-level template.tsx, I'd have expected to see the logging on every navigation, whether it is nested or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants