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.

Path alias pattern causes conflict between live and preview

See original GitHub issue

Hello !

I’m facing a weird problem :

Setting blog/[node:title] as the path alias pattern does not work: On the app, the URL localhost:3000/blog/slug resolves to a 404, but localhost:3000/blog/blog/slug works but obviously isn’t correct. When I try to access the preview, the resolved URL is localhost:3000/blog/slug -> does not work (404)

When setting back the path alias pattern back to /[node:title] correctly resolves on localhost:3000/blog/slug. The preview then resolves to localhost:3000/slug instead of localhost:3000/blog/slug, resulting in a 404.

Project structure is :

nextjs
    |___pages
          |____index.js
          |____[...slug].js
          |____blog
                 |___index.js
                 |___[...slug].js

next-drupal version used is 1.1.1, drupal/next version is 1.0.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
shadcncommented, Feb 1, 2022

1. Update getStaticPaths in pages/blog/[...slug].js

export async function getStaticPaths(context) {
  const paths = await getPathsFromContext(["node--article"], context)

  // Delete the "blog" prefix for route.
  paths.map((path) => {
    path.params.slug.shift()

    return path
  })

  return {
    paths,
    fallback: "blocking",
  }
}

2. Pass in the prefix to getStaticProps

export async function getStaticProps(context) {
  const node = await getResourceFromContext("node--article", context, {
    prefix: "blog", // <-------------------------
    params: {
      include: "field_image,uid",
    },
  })
}

We have a prefix option we can use here. Unfortunately next does not provide much in context. If we had the dir info that would be easier.

I think we can add the prefix options to getPathsFromContext as well to make this a bit simpler.

Can you give this a try and let me know?

0reactions
Kroonaxcommented, Feb 1, 2022

This is actually pretty clever (even if a little bit clunky) and it works, too !

I totally forgot about the prefix option in getResourceFromContext since the context given to getStaticProps is so poor.

The dir thingy is something Next community has been asking since forever (like conditional rerendering fired by a Back Office for example haha)

You’re truly a life saver, thanks a lot !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pathauto prevents core path functionality [#2881280] - Drupal
Problem/Motivation When an entity whose path is not managed by pathauto's patterns is give a manual URL alias, the alias is not saved....
Read more >
Use taxonomy path for a node's path alias - Drupal Answers
All the references to path aliases based on taxonomy that I have found use the entity value of the term, which I don't...
Read more >
Learning the Basics - Gradle User Manual
If Gradle finds a module descriptor in a particular repository, it will attempt to download all of the artifacts for that module from...
Read more >
Change mappings and settings for a data stream - Elastic
This new data stream will contain data from your existing stream. You can use the resolve index API to check if the name...
Read more >
How to Use Kafka Connect - Getting Started
You would export this variable to define the list of paths to the ... a path to plugins can result in library conflicts...
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