Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections API: dev server #68

Closed
wants to merge 3 commits into from
Closed

Collections API: dev server #68

wants to merge 3 commits into from

Conversation

drwpow
Copy link
Member

@drwpow drwpow commented Apr 9, 2021

Changes

Gets collections API up and running.

  • Basic pagination
  • Advanced params/routing
  • TODO: build all URLs (next PR)

Testing

  • Tests are passing
  • Tests updated where necessary

Docs

  • Docs / READMEs updated
  • Code comments added where helpful

Change of plans—maintain parity with Snowpack and Vite because our Collections API will use a different interface
},
perPage: 3
};
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proof of concept here: if you want to learn how to use this, look in the examples/blog folder


const traverse: typeof babelTraverse.default = (babelTraverse.default as any).default;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷🏻 Babel why are you like this

const ast = babelParser.parse(createCollection, {
sourceType: 'module',
});
traverse(ast, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of cleanup for later, perhaps. Alongside our original __renderPage() transform, we‘re also doing one here for createCollection(). Reason is that this happens outside the script context, so we need to transform it separately. Overall not too bad as this will only happen once total per-page, but still something to revisit later.

@drwpow drwpow force-pushed the collections-api branch 4 times, most recently from 98c34f4 to 288aa15 Compare April 9, 2021 04:42
@drwpow drwpow changed the title [wip] Collections api Collections API: dev server Apr 9, 2021
return {
statusCode: 301,
location: reqPath + '/1',
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird little bug: it‘s unexpected behavior when you set a pageSize: [num], but you try and load a non-paginated route. This attempts to redirect you to page 1, but it doesn‘t seem to be working in dev. Will have to investigate, but you basically only hit this if you‘re doing something confusing.

Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments, but this is perfect for a demo. Awesome work, Drew!

// page
let title = 'Muppet Blog: Home';
let description = 'An example blog on Astro';

// collection
// note: we want to show first 3 posts here, but we don‘t want to paginate at /1, /2, /3, etc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could look into a config option in the createCollection API to customize pagination (at the very least, to choose whether page 1 is written to URL /posts/1 or /posts). Users most likely don't want duplicate /posts/index.html and /posts/1/index.html files written into their final build, and almost certainly don't want to maintain near-duplicate posts.astro and $posts.astro files to get it.

Happy to chat through this more though if I'm missing anything here

}

/** import.meta.glob() */
export function handleImportGlob(spec: string, { namespace, filename }: GlobOptions): GlobResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Astro is built on top of Snowpack, we shouldn't be overriding Snowpack's import.meta.glob support. Since that means something in Snowpack world, it would be confusing / hard to document if we overrode this to mean something else entirely in Astro.

Lets leave Snowpack's import.meta.glob and import.meta.globEager as-is, and just implement this as a single Astro API that lets an Astro component fetch content files locally (without resorting to fs). import.meta.fetchContent() for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am missing something, but when I use Snowpack‘s built-in import.meta.glob(), then it goes back through our Snowpack plugin, and I only get a __render() hook and none of the markup frontmatter, etc. Maybe you could show me how to load this properly tomorrow.

@drwpow drwpow mentioned this pull request Apr 9, 2021
4 tasks
@matthewp matthewp mentioned this pull request Apr 12, 2021
@drwpow
Copy link
Member Author

drwpow commented Apr 12, 2021

Superceded by #70

@drwpow drwpow closed this Apr 12, 2021
@drwpow drwpow deleted the collections-api branch April 12, 2021 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants