nuxt-community/nuxtent-module

Nuxt generated project has wrong axios call

Opened this issue · 4 comments

Hi guys,

I am using nuxtent to generate static site, here is my issue:

with such nuxtent config:

  content: [
    [
      'project/node/',
      {
        page: 'project/node/_slug',
        permalink: 'project/node/:slug',
        isPost: false,
        generate: ['get', 'getAll']
      }
    ]
  ]

and content folder:

content
  |- project
  | |- node
  | | |- post1.md
  | | |- post2.md

nuxt generatewill create something like

dist
  |- _nuxt
  | |- content
  | | |- project
  | | | |- node
  | | | | |- project.node
  | | | | | |- post1.json
  | | | | | |- post2.json

however, after I host the dist folder and want to check post1, axios will ask resource from https://domainname/_nuxt/content/project/node/project.node.post1.json
and cause an error since the right path should be
https://domainname/_nuxt/content/project/node/project.node/post1.json.
I can make thing work by adding a line in

const serializedPermalink = permalink.replace(allButFirstSlash, '.')

to replace the last dot to slash:

const allButFirstSlash = /(?!^\/)(\/)/g
const lastDot = /\.(?=[^.]*$)/
const serializedPermalink = permalink.replace(allButFirstSlash, '.').replace(lastDot,'/');

I think another solution is to rearrange how nuxtent generate nuxt routes in

routePages.push(page.permalink)

to make nuxt generate based on page path and content name

This feature request is available on Nuxt.js community (#c97)

Is a folder named project.node? If so then yea it uses regex to match routes so we'd need to improve the command but this is a pretty unusual case, not sure if it is even recommended to use periods in file names

Yeah. I don't think it is an unusual case. It seems like if the permalink contains path

in order to inject right path to the nuxt->generate->router config

db.findAll(req['query']).forEach(page => {
routePages.push(page.permalink)
assetMap.set(buildPath(page.permalink, dirName, buildDir), page)
})

then the generated nuxtent content folder in dist will contain periods in file names.
I dislike those periods either but I have no choice. As I said, maybe it's better to leave permalink out of path info, and inject both path and permalink in the code above

Anyway, thank you so much to create this tool, the normal SSR function works pretty well. But I have to use nuxt and nuxtent to generate static site, so...

I'm sorry I'm not fully understanding - is an error only occurring when you generate the content? If so then yes it deals with the build file.

The problem likely lies here:

const buildPath = (permalink, section, buildDir) => {
// browser build path
// convert the permalink's slashes to periods so that
// generated content is not overly nested
const allButFirstSlash = /(?!^\/)\//
const filePath = permalink.replace(allButFirstSlash, '.')
return join(buildDir, section, filePath) + '.json'
}

Feel free to do a PR and if you have questions you can ask them in the slack channel

cool, I will give it a try