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

Using parameters with preact-iso/router #298

Closed
smeijer opened this issue Jan 5, 2021 · 4 comments · Fixed by #354
Closed

Using parameters with preact-iso/router #298

smeijer opened this issue Jan 5, 2021 · 4 comments · Fixed by #354

Comments

@smeijer
Copy link

smeijer commented Jan 5, 2021

When using preact-iso/router, which is the default in wmr, it doesn't seem to be possible to use route parameters.

Am I missing something? What's the recommended approach here?

import hydrate from 'preact-iso/hydrate';
import { LocationProvider, Router } from 'preact-iso/router';
import Home from './pages/home/index.js';
import NotFound from './pages/_404.js';

export function App() {
  return (
    <LocationProvider>
      <div class="app">
        <ErrorBoundary>
          <Router>
            <Home path="/:page?" />
            <NotFound default />
          </Router>
        </ErrorBoundary>
      </div>
    </LocationProvider>
  );
}

The sample will always result in "not found". Removing the optional parameter from the home route, will make "home" work again.

-            <Home path="/:page?" />
+            <Home path="/" />
@developit
Copy link
Member

Hiya! I ran into this myself and threw a workaround together, but it's really gross. Instead, I think we should just land support for :param segments in Router itself.

@JoviDeCroock
Copy link
Member

Hey @smeijer

This has been implemented in #300 not merged/released yet but if you want you can try it out

@smeijer
Copy link
Author

smeijer commented Jan 7, 2021

Thanks for the heads up. I'll give it a shot.

@orzechowskid
Copy link

thanks for this issue and for the PR! I noticed that the following code functions correctly on main but on the wildcard-routes branch will throw a TypeError and claim that vnode.props is undefined:

const publicRoutes = {
  "/blog": lazy(() => import('./pages/blog.js')),
  "/blog/:id": lazy(() => import('./pages/blog/single-post.js'))
};
const privateRoutes = {
  "/settings": lazy(() => import('./pages/settings.js'))
};
export function App(serverData) {
  return (
        <main id="main">
          <Router>
            {Object.entries(publicRoutes).map(
              ([ path, Component ]) => <Component path={path} key={path} />
            )}
            {Object.entries(privateRoutes).map(
              ([ path, Component ]) => <PrivateRoute component={Component} path={path} key={path} />
            )}
            <NotFound default />
          </Router>
        </main>
  );
}

I believe this is due to <Router>'s child array not being flattened before exec() is called. the workaround is trivial of course but I figured I'd mention it anyway.

thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants