#populate getStaticPaths for dynamic routes from firestore

1 messages · Page 1 of 1 (latest)

tulip mortar
#

I am trying to populate my paths from a query to firestore. I can't figure out how to import the required packages to make the query. Is this possible or should I give up now?

---
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase-admin/firestore';

const firebaseConfig = {
...
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

const { item } = Astro.params;
const items = db.collection('lost-items');

const paths = [];

items.forEach((item) => paths.push({ params: { item: item.id } }));

console.log('staticPaths', paths);

export function getStaticPaths() {
  return paths;
}
---

<p>{item}</p>
austere cairn
#

Am I not sure but
how about this one ?

export async function getStaticPaths() {
  
  let paths = [];
  items.forEach((item) => paths.push(item))

  paths.map((post)=>{
    return [
        { params: post } },
      ]
  }
mighty surge
#

I think the issue may also be that getStaticPaths doesn’t have access to variables outside it (firebaseConfig, app etc. in this case). It does have access to import statements though. Moving everything apart from the import statements inside the getStaticPaths function might be necessary?

To avoid reinstantiating the Firebase app for each call, you could probably move some of this to a separate utility file, e.g.

// src/utils/firebase.js

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase-admin/firestore';

const firebaseConfig = {
...
};

const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
---
// src/pages/index.astro

import { db } from '../utils/firebase';

export async function getStaticPaths() {
  const items = await db.collection('lost-items');
  const paths = items
    .map((item) => ({ params: { item: item.id } }));
  console.log('staticPaths', paths);
  return paths;
}

const { item } = Astro.params;
---

<p>{item}</p>
tulip mortar
#

yeah i think my problem was using firebase/app for initializeApp instead of the firebase-admin version. I think firebase/app works in browser only

#

at least i've moved on to a permission issue which is good