content icon indicating copy to clipboard operation
content copied to clipboard

Does this work in SPA mode??

Open limjix opened this issue 6 years ago • 20 comments

Hello,

I am curious if this package works in SPA mode?

<template>
  <div>
    <h1>HELLO</h1>

    <article>
    <nuxt-content :document="doc" />
  </article>
  </div>
</template>

<script>


export default {
  components: {

  },
  async asyncData({$content}) {
    const doc = await $content('index').fetch();
    return {doc}
  }
}
</script>

This is my code, and somehow on my local machine this works....but I always thought asyncData only works for SSR Mode?

However, when I upload this to firebase hosting, it does not work anymore.

Trying to understand why.

limjix avatar Jun 15 '20 15:06 limjix

Hi @limjix

The actual issue of nuxt build with spa mode is that it created a dist/ directory where actually it can be used with nuxt start and a server.

If you want to use the spa mode and a static hosting, you need to use nuxt generate instead of nuxt build, this way, Nuxt Content will know that it has to embed the database and avoid calling the server middleware.

With Nuxt 2.13, you will simply have to set target: 'static' in your nuxt.config.js.

atinux avatar Jun 15 '20 16:06 atinux

@Atinux Why is there no documentation or "help" section on this? I have an app that has been SPA for over a year. I did all the "requirements" and got everything working in development... deployed to my app to my server to be hosted and get blasted with 404s with no clue in the world as to why.

Also it's not exactly feasible to just use a development version of nuxt or to use generate because other nuxt modules like i18n have problems with generate. Please provide a working example of how to use SPA please.

jameshhood avatar Jun 15 '20 21:06 jameshhood

Well give a reproduction of what you are describing @jameshhood because it should work

atinux avatar Jun 15 '20 23:06 atinux

@Atinux I just followed the installation steps of the document and left everything default. I added my content folder and some MD files and everything is fine in development. When I run a build and deploy to my server, it 404s. I've even tried copying my folder into the /dist folder but it didnt work. I guess I need to try /_content since thats the default, but why this doesn't work out of the box for SPAs and why there doesn't seem to be anything in the documentation about it is why I'm saying that there should be something in the documentation about it.

jameshhood avatar Jun 16 '20 13:06 jameshhood

By copying the content folder to dist/_content I was able to get the 404s to go away, however, none of the content is rendering correctly. I have vue written in my markdown files and text and variables are working, but vue is not being compiled properly. Guessing this all has to deal with SPA...Since this really only works with static out of the box, there really needs to be examples or disclaimers on SSR and SPA usages.

jameshhood avatar Jun 16 '20 20:06 jameshhood

I am seeing similar issues, except we are trying to build in universal / ssr mode. After we run nuxt build, we take the pre-compiled .nuxt folder and deploy it without the source code. This approach works, except when we add the nuxt/content module. We get an error like this:

 ERROR  inject('content', value) has no value provided                                                                                                                                                                              20:18:10

  at inject (.nuxt/index.js:154:0)
  at plugin_server (.nuxt/content/plugin.server.js:9:0)
  at createApp (.nuxt/index.js:222:0)

Workaround is that we copy over the source folder (specifically our .md files), copy over the nuxt config, and then things work.

Seems like packaging of nuxt-content isn't really ready for a standalone package of ssr build, or am I wrong?

MatejBalantic avatar Sep 18 '20 18:09 MatejBalantic

Since the markdownfiles are parsed at nuxt start you have to deploy the content folder. This is nice so when your content folder changes you only stop the nuxt start process and start it again.

mathe42 avatar Sep 18 '20 20:09 mathe42

@mathe42 Thanks for the answer. While I do get your point that this is better for content reloading, parsing files at nuxt start works against the idea that the project can be prepackaged with nuxt build

In our case, it adds several seconds to the startup time of the AWS lambda that runs the SSR instance. That just kills the ability to use AWS Lambda for the purpose.

MatejBalantic avatar Sep 21 '20 09:09 MatejBalantic

I'm running server mode and encountering the same exception @MatejBalantic is encountering. nuxt-content works locally (nuxt start) but not when deployed to Vercel. Commenting out the @nuxt/content module in nuxt.config.js gets rid of the problem.

 ERROR  inject('content', value) has no value provided
  at inject (.nuxt/index.js:149:0)
  at plugin_server (.nuxt/content/plugin.server.js:9:0)
  at createApp (.nuxt/index.js:209:0)
  at async module.exports.__webpack_exports__.default (.nuxt/server.js:81:0)

seeARMS avatar Sep 25 '20 06:09 seeARMS

I'm using Vercel. It looks like Vercel doesn't support nuxt/content: https://github.com/nuxt/vercel-builder/issues/328#issuecomment-682096222

seeARMS avatar Sep 26 '20 23:09 seeARMS

Hi @MatejBalantic @seeARMS Have you got the solution now? I also experienced the same thing when I uploaded to a hosting service. However, if I turn off the @nuxt/content module everything works fine.

febrihidayan avatar Sep 29 '20 14:09 febrihidayan

Hi @febrihidayan, our solution was to move away from nuxt/content and use markdown-it to load our MD files

MatejBalantic avatar Sep 29 '20 16:09 MatejBalantic

Thanks @MatejBalantic, I seem to prefer to use marked

febrihidayan avatar Sep 29 '20 16:09 febrihidayan

I know this has been a while but the bug is still prevalent, @MatejBalantic how did you go about swapping from nuxt content to markdown-it and will I have to but any changes in my current md files? thanks!

Rio7z avatar Dec 17 '20 04:12 Rio7z

I am working to add support for serverless actually, to avoid scanning on Nuxt-start and improve the boot up time.

atinux avatar Dec 17 '20 05:12 atinux

I personally switched to using Prismic. It's been great for what I need out of a CMS.

jameshhood avatar Dec 17 '20 17:12 jameshhood

@seeARMS @MatejBalantic

We switched from static (generate) to SSR and had the same issue. With the config below we can load .md files in asyncData the usual way without a problem. The /_content/ API is available too.

{
  "builds": [
    {
      "src": "nuxt.config.js",
      "use": "@nuxtjs/vercel-builder",
      "config": {
        "serverFiles": [
          ".nuxt/content/**",
          "content/**"
        ]
      }
    }
  ]
}

pk992 avatar Jan 10 '21 19:01 pk992

I am seeing similar issues, except we are trying to build in universal / ssr mode. After we run nuxt build, we take the pre-compiled .nuxt folder and deploy it without the source code. This approach works, except when we add the nuxt/content module. We get an error like this:

 ERROR  inject('content', value) has no value provided                                                                                                                                                                              20:18:10

  at inject (.nuxt/index.js:154:0)
  at plugin_server (.nuxt/content/plugin.server.js:9:0)
  at createApp (.nuxt/index.js:222:0)

Workaround is that we copy over the source folder (specifically our .md files), copy over the nuxt config, and then things work.

Seems like packaging of nuxt-content isn't really ready for a standalone package of ssr build, or am I wrong?

@MatejBalantic Sorry for digging out such an old topic, but how excactly did you solve this? I tried this but without a good result. Where did you copy your content folder at? /app/content /app/.nuxt/content /app/.nuxt/dist/client/content

It seems that content does not work with standalone mode :(

janpfischer avatar Dec 19 '21 18:12 janpfischer

same problem in nuxt v3 and content v2 it seems. https://github.com/nuxt/content/issues/1154#issuecomment-1138766797

whenever I generate the SPA app - none of the markdown content gets copied:

export default defineNuxtConfig({
    ssr: false, // Disable Server Side rendering
    //...
})
i Prerendering 4 initial routes with crawler                                                                                                                                                                                                                                                            nitro 10:34:56  
  ├─ / (13ms)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     nitro 10:34:56  
  ├─ /200 (1ms)                                                                                                                                                                                                                                                                                         nitro 10:34:56  
  ├─ /404 (1ms)                                                                                                                                                                                                                                                                                         nitro 10:34:56  
  ├─ /api/_content/cache (66ms)  

the result is that none of the pages could be queried nor viewed.. I was surprised to find this thread from 2020 and seeing that there is no proper fix yet.. or??

@Atinux seeing that it still causes trouble for people, do you have anything to comment or to add on the issue?

pavlexander avatar May 27 '22 07:05 pavlexander

What's funny is I'm now migrating my project from nuxt/vue 2 to 3 and I'm running into this same issue almost 5 years later and I still cant get this to work with using nuxt/content 3. I just wanted to embed a simple changelog into my app but what I just did was add the markdown file in my public directory and use the markdown-it package to render the markdown. Still not super ideal but at least it works.

jameshhood avatar Mar 18 '25 14:03 jameshhood