birkir / gatsby-source-prismic-graphql

Gatsby source plugin for Prismic GraphQL
MIT License
137 stars 75 forks source link

How to use this in a hybrid application with client-only routes #158

Open classiemilio opened 4 years ago

classiemilio commented 4 years ago

I'm trying to use this alongside gatsby-plugin-create-client-paths for a hybrid application with some client-only routes. I'm setting up gatsby-plugin-create-client-paths to treat all routes that match "/app/*" as client paths. However, it seems to not work alongside this plugin if I have any Prismic pages that match "/:uid" (even though none of the pages in my Prismic instance have "app" as their uid). Things work fine if I change my pages to match something else, like "/page/:uid", but I'd prefer to keep the top-level path for my general pages. Is there a way to make this work?

I currently have this in my gatsby-config.js for those two plugins (and I've tried switching up their order just in case):

{
  resolve: 'gatsby-source-prismic-graphql',
  options: {
    repositoryName: 'REPO_NAME',
    accessToken: 'TOKEN',           
    pages: [
      { 
        type: 'Page', 
        match: '/:uid',
        path: '/preview/page',
        component: require.resolve('./src/templates/page.js'),
      },
      { 
        type: 'Blog_post',
        match: '/blog/:uid',
        path: '/preview/blog',       
        component: require.resolve('./src/templates/blogPost.js'),
      },
    ]
  },
},
{
  resolve: 'gatsby-plugin-create-client-paths',
  options: { prefixes: ['/app/*'] },
},
classiemilio commented 4 years ago

I was able to do this by forking this project, bumping path-to-regexp to version 6.1.0 and using negative lookahead in the match param for my top-level pages. The version of path-to-regexp this project is currently using doesn't support negative lookaheads. This works for my purposes after bumping that dependency:

pages: [
  { 
    type: 'Page', 
    match: "/:uid((?!app)[^/]+)",
    path: '/preview/page',
    component: require.resolve('./src/templates/page.js'),
  },
...

I've submitted a pull request bumping that dependency, would appreciate if a maintainer could merge it, thanks!

cc @birkir