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.

No docs for Getting URL params

See original GitHub issue

The doc here isn’t useful since it doesn’t use chunks:

{
  path: '/tasks/:id',
  action({ params }) {
    const resp = await fetch(`/api/tasks/${params.id}`);
    const data = await resp.json();
    return data && {
      title: data.title,
      component: <TodoItem {...data} />
    };
  }
}

This assumes <TodoItem /> is already loaded, which is exactly what I DON’T want to do.

Where can I see a full example of this implementation?

I tried many different variations with action and none works.

I can make a PR with decent docs with some guidance 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
frenzzycommented, Oct 20, 2017
/* src/routes/index.js */
const routes = [
  // ...
  {
    path: '/blog/:post',
    load: () => import(/* webpackChunkName: 'blog' */ './blog'),
  },
  // ...
};
/* src/routes/blog/index.js */
import BlogPost from './BlogPost';

async function action({ params }) {
  // read more about dynamic import with webpack here:
  // https://webpack.js.org/api/module-methods/#import
  // if following does not work, try `import('./posts/' + params.post + '.js')`
  const post = // i.e. get `./posts/coffee.js`
    await import(/* webpackChunkName: "post-[index]" */ `./posts/${params.post}.js`)
    .then(module => module.default) // use an object from `export default`
    .catch(error => {
      if (error.message.startsWith('Cannot find module')) {
        return null; // module (post) does not exists
      }
      throw error; // loading chunk failed (go to error handler)
    });
  if (!post) { // post not found
    return null; // go to next route (or render 404)
  }
  return { title: post.title, component: <BlogPost post={post} /> };
}

export default action;
/* src/routes/blog/BlogPost.js */
const BlogPost = ({ post }) => (
  <div>
    <h1>{post.title}</h1>
    <p>{post.body}</p>
  </div>
);

export default BlogPost;
/* src/routes/blog/posts/coffee.js */
export default {
  title: 'Coffee',
  tags: [],
  body: 'body of post here',
}
0reactions
goldyluckscommented, Oct 20, 2017

I’m new to route code splitting, how is this possible?

how can webpack prepare the chunks without knowing them at compile time?

In anyway I think it will be great addition to the docs, along with a nested route example.

Thanks @frenzzy , YOU ROCK!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the URL without any parameters in JavaScript?
Use the URL() constructor, then extract and concatenate the origin and pathname. This will automatically strip the search (aka query) parameters from the ......
Read more >
How to Get URL Parameters with JavaScript - SitePoint
In this article, we'll show you how to parse and manipulate URL parameters using JavaScript. Getting a URL Parameter. In modern browsers, this ......
Read more >
How to Get URL Parameters - W3docs
Read this JavaScript tutorial and learn how you can easily get the URL parameters with the help of the methods provided by the...
Read more >
URLSearchParams - Web APIs - MDN Web Docs
Chrome Edge URLSearchParams Full support. Chrome49. Toggle history Full support. Edge... @@iterator Full support. Chrome49. Toggle history Full support. Edge... URLSearchParams() constructor Full support. Chrome49. Toggle...
Read more >
Spring cleaning: the URL Parameters tool - Google Developers
In short: We're deprecating the URL Parameters tool in Search Console in 1 month. There's no action required from the current users of...
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