Skip to content

Fech data from an external app #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
daishizenSensei opened this issue Jan 27, 2021 · 15 comments
Closed

Fech data from an external app #736

daishizenSensei opened this issue Jan 27, 2021 · 15 comments
Labels
enhancement New feature or request question Further information is requested Stale

Comments

@daishizenSensei
Copy link

Hello everyone,

We use nuxt content for our content management. And we have to build another app in a new repo, but we have to fetch content of our first app. Is is possible to open an endpoint in production? Or maybe possible to achieve that with another cms git?
Hope that we are not blocked now with that... :(

Thanks for your help,
Kevin

@daishizenSensei daishizenSensei added the question Further information is requested label Jan 27, 2021
Copy link
Member

atinux commented Feb 1, 2021

Hi @kbrosseau

We plan to add this possibility very soon.

@atinux atinux added the enhancement New feature or request label Feb 1, 2021 — with Volta.net
@daishizenSensei
Copy link
Author

daishizenSensei commented Feb 1, 2021

Hi @atinux, Great!

@cschweda
Copy link

cschweda commented Feb 8, 2021

One way to do this now (until it's actually a feature) is to run a node script before the build starts to (a) fetch content from one or more external endpoints, (b) save everything into your app's /content directory, and then (c) generate the Nuxt app.

@madsh93
Copy link

madsh93 commented Feb 11, 2022

@atinux is this feature planned for v2?

Copy link
Member

atinux commented Feb 14, 2022

It's planned yes 😀
We will need it in order to merge all of our modules documentation into one website.

@Clex1o1
Copy link

Clex1o1 commented Feb 7, 2023

Hi all, I ran into the same problem. While @cschweda's solution only works once in the build step (I guess) I solved it by using the markdown-it library in my component directly:

<script lang="ts" setup>
import MarkDownIt from "markdown-it";

const md = new MarkDownIt({ html: true });

const props = withDefaults(
  defineProps<{
    Text: string;
  }>(),
  {}
);
</script>
<template>
  <article class="prose" v-html="md.render(props.Text)"></article>
</template>

@zefman
Copy link

zefman commented Jun 14, 2023

Bump. Would love this feature! Our markdown is in a CMS for a lot of reasons and it would be awesome to use Nuxt content for the rendering and all the features that come with it.

Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 10, 2025
@Syndesi
Copy link

Syndesi commented Feb 10, 2025

Just a comment to keep it open :)

@cschweda
Copy link

yes, please keep this open. I've solved my issue with Nuxt Content with the pre-build step I mention above, but I'd love to see some sort of configurable 'fetcher' for external content.

Happy to provide an example of my pre-build code if anyone is interested.

@github-actions github-actions bot removed the Stale label Feb 10, 2025
@adamdehaven
Copy link
Contributor

I'd love to see some sort of configurable 'fetcher' for external content.

Are you wanting to fetch the external content during the build so that it can then be statically generated, or do you want fetching done at runtime so that the initial page can be loaded from the server, then subsequent pages can be fetched from the client?

@cschweda
Copy link

cschweda commented Feb 10, 2025

I'd love to see some sort of configurable 'fetcher' for external content.

Are you wanting to fetch the external content during the build so that it can then be statically generated, or do you want fetching done at runtime so that the initial page can be loaded from the server, then subsequent pages can be fetched from the client?

Doesn't really matter, right? All that matters is when the page is rendered the content is there. How it happens? Don't care.

What I do now is -- as I say above -- do a pre-build process that (a) grabs the data from the cms, (b) inserts it into files in content, and then (c) renders the content on the page.

It works well -- no issues -- but I'm never sure if a pre-build step is a best-practice type-thing -- or if it's just a hack. I'm sensing it'a a hack -- and that's fine -- it works -- but I would like a more elegant option.

EDIT: Btw, I use Strapi -- so it's super-easy to get the content during the pre-build. That's not the issue. The issue is if there's a way to do it directly in Nuxt Content/

@adamdehaven
Copy link
Contributor

adamdehaven commented Feb 11, 2025

If you're fetching content before building (i.e. generate) and the content itself is static, I don't think there's anything wrong with using a build hook. You could trigger this whenever your pages are created/edited/etc., re-deploy, and you're good to go. This sounds essentially like your existing setup and supports deploying a fully-static, pre-rendered site.

While content v3 supports a remote source such as a GitHub repository, again, as a fully-static, pre-rendered site, there is another more custom solution you could set up assuming the following:

  • Your content can be returned from your remote source as raw MDC content
  • You don't need full site generation
  • It's acceptable to render the first page request on the server at runtime; all other pages would then be fetched and rendered in the client
  • Shown below is pseudo-code, adjust for your usage

  1. Similar to the instructions for Implementing Document Driven mode, create a catch-all page component pages/[...slug].vue that will serve all of your pages, regardless of path.

  2. Inside the page component, fetch the content from your external source, and manually parse and render in your component via @nuxtjs/mdc (this is what does the parsing under the hood in Nuxt Content):

    <template>
      <div v-if="ast">
        <MDCRenderer
          v-if="ast?.body"
          :body="ast.body"
          :data="ast.data"
        />
      </div>
    </template>
    
    <script lang="ts" setup>
    import { parseMarkdown } from '@nuxtjs/mdc/runtime'
    
    const route = useRoute()
    const currentPath = computed((): string => route.path)
    const { data: ast } = await useAsyncData(`parsed-page-${currentPath.value}`, async () => {
      // Call to your external API  
      const strapiMarkdown = await $fetch(`https://example.com/api/content?pagePath=${currentPath.value}`)
      return parseMarkdown(strapiMarkdown)
    }, { 
      watch: [currentPath] 
    })
    </script>

You'll need to account for loading and error states, etc. and ensure you install @nuxtjs/mdc and initialize it as a module in your nuxt.config.ts.

If you configure your app to render via SSR, you can do an extra step and configure routeRules in your nuxt.config.ts for proper caching, swr behavior, etc.

Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Apr 12, 2025
Copy link

This issue was closed because it has been stalled for 30 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

8 participants