#Netlify build error using dynamic routes

49 messages · Page 1 of 1 (latest)

faint ledge
#

I'm not sure what to make of this error when trying to build on Netlify.

3:26:06 PM:  error   Expected "category" to be a string
3:26:06 PM:   Code:
3:26:06 PM:       236 |             var typeOfMessage = repeat ? "an array" : "a string";
3:26:06 PM:     > 237 |             throw new TypeError("Expected \"".concat(token.name, "\" to be ").concat(typeOfMessage));
3:26:06 PM:           |                   ^
3:26:06 PM:       238 |         }
3:26:06 PM:       239 |         return path;
3:26:06 PM:       240 |     };
3:26:06 PM:     
#

things work as expected with ntl dev

#

when I run npx astro build I'm getting this error, which is different

▶ src/pages/index.astro
 error   Cannot destructure property 'blogPostCollection' of 'response' as it is undefined.```
#

this is the code from my file pulling in content from Contentful

  const posts = await getContentfulContent({ query: allPosts });

  let paths = posts.blogPostCollection.items.map((post) => {
    return {
      params: { slug: HELPER.removeSlash(post.slug) },
      props: { post },
    };
  });
  return paths;
}

const { post } = Astro.props;```

**EDIT:** the reason the above isn't working is because my env variables are stored in Netlify and `npx astro build` isn't getting them to call Contentful successfully
steel crag
#

Maybe a silly question, but are you sure all necessary environment variables for accessing Contentful are available to your Netlify build? Expected "category" to be a string" sounds like getStaticPaths() wasn't able to receive the contents from getContentfulContent -- could explain why it works in dev, but doesn't explain your local build error running npx astro build.

faint ledge
#

actually... it's even sillier. I didn't have the Netlify SSR adapter installed 😭

steel crag
#

Lot's of little details in these kinds of projects! Happens to me often haunted

faint ledge
#

yeah. but now here's a question. after enabling the adapter, I have the output path in my astro config set to dist/blog, but it's still building to dist/. before the adapter it was properly building the dist/blog

steel crag
#

Do you have a netlify.toml?

faint ledge
#

I do

#

I guess I need to set that up properly

steel crag
#

netlify.toml:

[build]
  # Directory to change to before starting a build.
  # This is where we will look for package.json/.nvmrc/etc.
  # If not set, defaults to the root directory.
  base = "project/"

  # Directory that contains the deploy-ready HTML files and
  # assets generated by the build. This is relative to the base
  # directory if one has been set, or the root directory if
  # a base has not been set. This sample publishes the directory
  # located at the absolute path "root/project/build-output"

  publish = "build-output/"
#

I believe you'll need to make sure to set it there

faint ledge
#

yeah. messing with this now. my setup has been a real struggle. I'm trying to get the site to build on the netlify server at www.site.com/blog and not at www.site.com but can't seem to figure out the right path forward

#

we're using different stacks for our www site and blog. so battling reverse proxy stuff and I'm losing my mind 😂

steel crag
#

yikes -- sounds scary!

faint ledge
#

[SOLVED] Netlify build error using dynamic routes

faint ledge
#

Netlify build error using dynamic routes

#

well this is a mess. so the adapter wasn't necessary and didn't fix this problem

mighty harbor
#

Oh wow, this is very strange...

faint ledge
#

so disregard the npx astro build errors. that's no longer relevant, but I didn't want to delete to comments

mighty harbor
#

Alright, what's the error now?

faint ledge
#

the original posted error at the top

mighty harbor
#
  let paths = posts.blogPostCollection.items.map((post) => {
    return {
-      params: { slug: HELPER.removeSlash(post.slug) },
+      params: { category: HELPER.removeSlash(post.slug) },
      props: { post },
    };
  });
#
3:26:06 PM: ▶ src/pages/[category]/index.astro
3:26:06 PM:  error   Expected "category" to be a string

means that params should return a property named "category"

faint ledge
#

so the error you're referencing there is the error from running npx astro build. and slug there is actually correct.

mighty harbor
#

But the file looks to be src/pages/[category]/index.astro? So you need to return a value for the category param

faint ledge
#

this is the code in [category]/index.html

import { allPosts } from "../../utils/queries.js";
import { getContentfulContent } from "../../utils/contentful.js";
import BaseLayout from "../../layouts/BaseLayout.astro";
import CategoryLayout from "../../layouts/CategoryLayout.astro";

const { post } = Astro.props;

export async function getStaticPaths() {
  return [
    { params: { slug: "internal-comms" } },
    { params: { slug: "product" } },
    { params: { slug: "leadership" } },
  ];
}
---

<BaseLayout>
  <CategoryLayout />
</BaseLayout>```
#

I'm just using static params to test locally

mighty harbor
faint ledge
#

oh shit...

#

omg

#

🫣

mighty harbor
#

slug is not a magical thing. The params object needs to match the names inside the [dynamic-path]

faint ledge
#

why does this always happen when I don't step away from the computer when I should.

mighty harbor
#

Lol no worries! I bet our docs/error messages could be a bit more helpful here 🙂

faint ledge
#

wild that it works in dev

#

I'm 2/2 in edge cases for docs today 😂

mighty harbor
#

Hmm, why would it work in dev?

faint ledge
#

I don't know, but it works in dev

mighty harbor
#

Oh maybe we're not enforcing that it matches something returned from getStaticPaths?

#

I'll open an issue about that

faint ledge
#

wait I lied. just realizing that the stackblitz I was running was category and my vscode was using slug

#

ugghhhhh

#

I need to get up.

mighty harbor
#

Ohhh OK that's significantly less confusing!

#

But yeah, things like this are hard to catch! I bet our error message could help guide you to the right answer, though

faint ledge
#

I'll leave that up to you fine devs to decide. thank you for taking the time though. one of these days when I'm an expert I'll join support and give back