nuxt / nuxt/nuxt

Managing page transition state in Nuxt

Open
#30,968 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
60.9k
Forks
5.8k
Avg merge
7h 8m
Merged PRs (30d)
162

Description

Hello,

I found a Nuxt app example using GSAP that attempts to implement a hook after a page transition, with a shared ref across the whole application.
The idea is to have something like a lifecycle hook similar to 'page:transition:finish' (onAfterLeave event) but instead with 'page:transition:after' (onAfterEnter event).
GSAP Page Transitions example: https://stackblitz.com/edit/nuxt-starter-bthjlg?file=composables%2Ftransition-composable.js

The GSAP dev implemented a composable like this:

const transitionState = reactive({
  transitionComplete: false,
});

export const useTransitionComposable = () => {
  const toggleTransitionComplete = (value) => {
    transitionState.transitionComplete = value;
  };

  return {
    transitionState,
    toggleTransitionComplete,
  };
}

If I understand the Nuxt documentation correctly (https://nuxt.com/docs/getting-started/state-management), this approach should be avoided.

First question:
Is this behavior the same across all rendering modes? (SWR, ISR, SSR, SSG, SPA)

Second question:
Following the documentation, I ended up with a composable like this:

const useTransitionCompleteState = () => useState<boolean>('isTransitionComplete', () => shallowRef(false))
export default = () => {
  const isTransitionComplete = useTransitionCompleteState()
  const toggleTransitionComplete = (value: boolean) => {
    isTransitionComplete.value = value
  };

  return {
    transitionState: readonly(isTransitionComplete),
    toggleTransitionComplete,
  }
}

Last question:
The following implementation is more concise and works on my end. Is it a good approach, or does it introduce issues? What’s the difference?

export default = () => {
  const isTransitionComplete = useState<boolean>('isTransitionComplete', () => shallowRef(false))
  const toggleTransitionComplete = (value: boolean) => {
    isTransitionComplete.value = value
  }

  return {
    transitionState: readonly(isTransitionComplete),
    toggleTransitionComplete,
  }
}

Thanks in advance for your help! :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Nuxt state-management documentation and the linked GSAP Page Transitions example, then compare the two composable implementations described in the issue. Clarify their behavior across SWR, ISR, SSR, SSG, and SPA modes, and document which approach is recommended and why.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxtjs, typescript
Domain
frontend
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.