#Displaying Content From import.meta.glob

1 messages · Page 1 of 1 (latest)

icy canopy
#

I have a site with over 15,000 static pages. I am able to load them individually using a url no problem. I would like to implement a "Random" page, where I would pick a random identifier from a pre-existing search index, and then use that to load one of the pages. My rough implementation in Svelte is:

<script>
  import { onMount } from "svelte";
  import Layout from '../layouts/sign/single.astro'
  let randomNumber = 0;
  let randomFile = null;

  let endpoint =
    "SEARCH_INDEX_QUERY_THAT_RETURNS_A_COUNT";
  onMount(async function () {
    const response = await fetch(endpoint);
    const data = await response.text();

    const totalCount = parseInt(data);

    randomNumber = Math.floor(Math.random() * totalCount);

    const files = import.meta.glob("../pages/sign/*.md");

    let counter = 0;
    for (const path in files) {
      if (counter == randomNumber) {
        randomFile = await files[path]();
        console.log(randomFile);
      }
      counter++;
    }
  });
</script>

{#if randomFile}
  <randomFile/>
{:else}
<p>Loading</p>
{/if} 

The code is correctly returning a random page when being run, however, I am unsure how and if I can actually display the page. Each page does have a layout defined. I am calling this svelte component using an page that looks like:

---
import Random from "../components/Random.svelte"

---
<Random client:load/>

I have a few other complicated scenarios in mind that may also use this approach. The general idea is that all of the pages are stored statically - but these static pages could be pulled up dynamically at run time using a search query, or in this case a random search.

lavish warren
#

I think you got the right Idea with that @icy canopy

#

🙃

icy canopy
#

The part that I am stuck on is while the code compiles and runs. When I display the <randomFile> nothing actually renders. No console errors (browser or in the terminal running astro). The randomFile component that it is pulling from does have a layout associated - so I would expect to see something or an error.