d3xter-dev / sitemap-module-nuxt-3

Sitemap Module for Nuxt 3
https://sitemap.nuxtjs.org
MIT License
63 stars 12 forks source link

Having Trouble With Dynamic Import #14

Open SleepiestAdam opened 2 years ago

SleepiestAdam commented 2 years ago

I'm using Nuxt 3 release candidate 13 and just installed sitemap-module-nuxt-3 today.

I'm trying to make a call to our REST api that just returns a standard array of strings / dynamic routes.

My dynamicRoutes.js file is exactly as the example:

export default async () => {
  return await $fetch('/api/sitemap_routes', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
  });
};

My sitemap_routes file imports my back-end functions, one of which is getRoutes, which simply calls the RestAPI and returns the array of strings.

import { backendServices } from '~/services/backendServices';

export default defineEventHandler(async (event) => {
    return await backendServices.getRoutes();
})

Unfortunately it seems whenever I try use backendServices in my sitemap_routes.js file, it fails to load the routes.

If I change my sitemap_routes to something like this:

import { backendServices } from '~/services/backendServices';

export default defineEventHandler((event) => {
    return ["/content/some-content"];
})

Then it works fine...

The backendServices function "getRoutes" is working fine in other parts of the app, and I've confirmed it's correctly returning an array of routes, so I'm a little stumped as to why it fails to work when I make it async and try have it fetch the routes from the REST api.

danksdeklerk commented 1 year ago

Hi SleepiestAdam

Not sure what the actual error is that you are getting and if this will help, but I did find that if I use the spread operator when returning the array it resolved the following error: "undefined is not iterable (cannot read property Symbol(Symbol.iterator))"

e.g

let siteMapUrls = []
...<populate array from rest api call>...
return [...siteMapUrls]

This fixed the problem when using a function to populated the 'routes' value for the sitemap

nuxt.config.ts
     sitemap: {
          routes:   

But when I also use the function under

     generate: {
          routes:

I for some or other reason get the following error: _ctx.route.includes is not a function

at node_modules/@funken-studio/sitemap-nuxt-3/dist/module.mjs:418:24 at node_modules/hookable/dist/index.mjs:39:82 at async prerender (node_modules/nitropack/dist/shared/nitro.c8278d90.mjs:2961:7) at async node_modules/nuxt/dist/index.mjs:1675:7 at async build (node_modules/nuxt/dist/index.mjs:2236:5) at async Object.invoke (node_modules/nuxi/dist/chunks/build.mjs:56:5) at async Object.invoke (node_modules/nuxi/dist/chunks/generate.mjs:32:5) at async _main (nodemodules/nuxi/dist/cli.mjs:50:20)

driven-group commented 1 year ago

I was also getting the same error in RC.14: "undefined is not iterable (cannot read property Symbol(Symbol.iterator))" When I used the spread operator it now just gives a standard: "500 dynamicRoutes is not iterable" I have also confirmed that it returns an array of strings and if I manually type an array into "routes" it works fine. It just isn't reading the array from the helper function. Not sure what else to try.

This is killing me.

edit: It seems to be giving an error at this line: "/node_modules/@funken-studio/sitemap-nuxt-3/dist/runtime/cache.mjs:11:40" at the async AsyncCache.load.

SOLVED Fixed it by initialising dynamicRoutes as an empty array first. Then set the content from an async api call and returned the array.

eg. File: /helper/dynamicRoutes.js

export default async () => { let dynamicRoutes = [] await $fetch('/api/endpoint') .then(res => dynamicRoutes = res.map(item=> /${item.value})) return dynamicRoutes }

liamcharmer commented 1 year ago

Anyone used supabase?