question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Router/navigation support

See original GitHub issue

If using Next.js and building both an iOS/Android app and a web app/PWA, you’ll need both support for Next.js routing and also quality client-side routing.

This way you can serve up any valid page by navigating directly to it on the web (say, /about), complete with static rendering and possibly even SSR, but also navigate to an about page client-side complete with transitions and quality native experience users expect.

I wanted to document what I think the best approach here is which is @creativiii’s hybrid next/local approach (see his post on this: https://ironeko.com/posts/animating-page-transitions-in-nextjs-for-capacitor?ref=last_articles)

App Shell

The core idea is that every page serves up an <AppShell> component that is essentially all of the Nav, Menu, Tabs, and PageStack needed to build the actual client app experience:

Screen Shot 2020-12-30 at 10 46 26 AM

Client-side routing

Then, instead of using Next.js for actual client-side navigation, we will use a simple client-side router like Wouter: https://github.com/molefrog/wouter

This will handle the “local, client-side” navigation and transitions, but won’t handle any “server-side” requests at all.

Handling Deeplinks

Most apps will want to handle universal links, but the way those URLs are handled is quite different from a product web server and a mobile app which is not running a web server.

Thus, the app will need to handle deeplinks (https://capacitorjs.com/docs/guides/deep-links) and process each request. If it detects that there is an in-app handler for that URL, it will navigate client-side to that page. If not, it will let the browser handle the URL.

Example page

This is an example of what the Next.js page code would contain:

import AppShell from '../components/AppShell';
import Home from '../components/pages/Home';

export default function Index() {
  return <AppShell page={Home} />;
}

usePage hook

Then, each page component can use the usePage hook to configure app shell related values, such as the title of the page, icons for the tabs, and other values

const Home = ({ selected }) => {
  usePage({
    title: 'Home',
  });

  return (
    ...
  )
}

Tasks

  • Abstract away page structure into AppShell component
  • Test/integrate client-side router like Wouter
  • Build simple navigation transitions
  • Verify static/SSR support along with client-side routing

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mlynchcommented, Dec 31, 2020

Turns out it’s not 😭 at least not officially. However I’m optimistic there’s a middle ground. At the very worst it could require two Next.js apps with one just for SSRing but sharing code. There are some hacky workarounds here that I’m going to look into: https://github.com/vercel/next.js/discussions/15674

At the very least the getStaticProps will work fine for now.

0reactions
mlynchcommented, Jan 25, 2021

Closing as I ended up going with a solution that primarily has Ionic handle routing client side (so you get all the nice transitions and history stack management), and next.js handles any initial load routes and renders the app shell. Works great for iOS and Android apps, not ideal for PWAs but this specific project is initially focused on app store apps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Router - Angular
A service that provides navigation among views and URL manipulation ... By default, the router re-uses a component instance when it re-navigates to...
Read more >
Angular Router: Navigation Using RouterLink, Navigate, or ...
In Angular, RouterLink is a directive for navigating to a different route declaratively. Router.navigate and Router.
Read more >
Angular Navigation: How Routing & Redirects Work in Angular ...
Our Angular Navigation guide covers how routing works in an app built ... to support multiple navigation stacks, relative URLs are something not...
Read more >
Router/navigation support · Issue #3 · mlynch/nextjs-tailwind ...
Just wanted to add my two cents on the third party router. While it does work for client side navigation I'm almost 100%...
Read more >
Understanding The Router's Navigation Cycle - Angular inDepth
Angular Router Series: Pillar 2 — Understanding The Router's Navigation Cycle ... way to see the navigation cycle is by subscribing to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found