I'm working on a client-side search for my blog. It works by building a search index in astro:build:done. I want to add metascraper, but the API is async. Is there a way to make astro:build:done an async function, or call async functions from it? Assume I know absolutely no JavaScript or TypeScript and I'm just muddling my way through 🙂
#How to call an async function from an integration?
37 messages · Page 1 of 1 (latest)
The example in the docs for astro:build:done is async https://docs.astro.build/en/reference/integrations-reference/#dir-option
hm, when i do that my editor doesn't complain about the async, but it does complain about putting an await inside of it
is my editor wrong? 😄
doesn't seem like it
error Transform failed with 1 error:
/Users/jordan/Source/jordemort.github.io/src/search/astro.ts:29:12: ERROR: "await" can only be used inside an "async" function
File:
/Users/jordan/Source/jordemort.github.io/src/search/astro.ts
Code:
"await" can only be used inside an "async" function
27 |
28 | const html = fs.readFileSync(htmlpath).toString();
29 | await indexer.index(url, html);
| ^
30 | }
31 | });
that's the integration
what's your code?
ah thanks
you need to make
pages.forEach(({ pathname }) => {
async
ooh
lol 😇
i seem to be having problems waiting for completion now though :/
here's my indexer: https://gist.github.com/jordemort/8ab5c2a8c8c52b3623553333b331db13
and when i build the site i get output like this: ```
Indexed https://jordemort.dev/blog/lwn-lxc-lxd-a-different-container-story/
Indexed https://jordemort.dev/blog/adding-copy-buttons-to-code-blocks/
Indexed https://jordemort.dev/blog/fixing-leaky-svg-style-tags/
Indexed https://jordemort.dev/blog/webmentions-and-pingbacks/
Indexed https://jordemort.dev/blog/caketop-website-launched/
Indexed https://jordemort.dev/blog/kill-all-the-boilerplate/
Indexed https://jordemort.dev/blog/fun-with-ccs-animation/
Indexed https://jordemort.dev/blog/tags-and-stylish-feeds/
Indexed https://jordemort.dev/blog/remark-all-the-things/
Indexed https://jordemort.dev/blog/feeds-five-ways/
Indexed https://jordemort.dev/blog/new-blog-here/
Wrote search index to /Users/jordan/Source/jordemort.github.io/dist/index.sqlite3
12:25:50 PM [build] 56 page(s) built in 4.56s
12:25:50 PM [build] Complete!
metadata? {
description: 'My portfolio & some occasional blogging',
title: 'Jordan Webb'
}
metadata? { description: 'Things that I have done', title: 'Portfolio' }
metadata? {
description: 'Development notes and technical musings',
title: 'Jordan Webb’s blog'
}
the stuff after let metadata = await scraper({ html, url }); doesn't fire until after astro thinks the build is done?
and it doesn't seem like the insertEntry after that ever runs
no console.log("Indexed %s with metascraper", url); lines
yeah so that's another caveat of using .forEach
you need to wrap it inside promise if you're using foreach with async
just do a for loop instead
and that'd work fine
this change to be done inside: https://gist.github.com/jordemort/7c4cb1af0897b4fbb1c1a5cdf3d0b501#file-astro-ts-L19
afaik, @slow seal feel free to dive in!
ah ok
switched it to ```
for (var i = 0; i < pages.length; i++) {
const pathname = pages[i].pathname;
...
}
and that worked, is there a more elegant way?
for (const page in pages) {
const pathname = page.pathname
}
my editor seems to think page in for (var page in pages) will be a string and when i console.log it it is 0 which is suprising
You probably have to type the pages so your editor knows what type it is
yeah it really seems like for (const page in pages) { just gets me array indexes into pages, not the actual objects in pages itself
which is fine, i can work with that
thanks for your help!
awesome @tepid siren !