Closed axelthat closed 1 year ago
Hi @axelthat I guess you can't just add the mdx files into the route directory and let quick load and resolve them as described here, right? that would be pretty easy to achieve.
The error just states, that you can not import something dynamically within a part of the code which gets separated by the qwik optimizer. So actually all functions ending with a dollar sign ($
) will be splitted into chunks at build time which then can be streamed on demand. Therefore the optimizer has no chance to guess what you want to load at runtime.
I played around a bit with it and the only solution i can think of atm looks like this (uses qwik-city@0.1.0-beta9
):
import { component$, Fragment } from "@builder.io/qwik";
import { loader$ } from "@builder.io/qwik-city";
export const getBlogPost = loader$(async ({ params }) => {
const { slug } = params;
// NEEDS TO BE UPDATED MANUALLY WHICH IS KINDA CUMBERSOME
const registry = {
foo: () => import("../../blog/foo.mdx"),
bar: () => import("../../blog/bar.mdx"),
baz: () => import("../../blog/baz.mdx"),
};
const data =
slug in registry && typeof registry[slug] === "function"
? await registry[slug]()
: null;
return {
slug,
frontmatter: data?.frontmatter,
default: JSON.stringify(data?.default()?.props?.children),
};
});
export default component$(() => {
const blogPost = getBlogPost.use();
const blogData = blogPost.value;
return (
<Fragment>
<h1>blog {blogPost.value.slug}</h1>
<h2>{blogData.frontmatter.title}</h2>
{JSON.parse(blogData.default)?.map((item) => (
<p>{item.props.children}</p>
))}
</Fragment>
);
});
Very ugly and quick'n'dirty implemented but maybe it helps you to find a solution that works for you. i would recommend to use the built-in solution mentioned on top 👼
Hi @axelthat just wanted to check about the state of this issue. have you been successful? 🤞
@zanettin Sorry for late reply. Yes, that works.
thanks @axelthat and @zanettin
closing
I don't think this solution works anymore: #5100. Using import()
on something other that a route throws an error.
Which component is affected?
Qwik City (routing)
Describe the bug
I want to load markdown content based on the parameter from the url. If the url is
/blogs/blog1
, I would like to load the content from/src/content/blogs/blog1.mdx
. But it is giving me this error,[plugin:vite-plugin-eslint] Data returned from the onGet() endpoint must be serializable so it can also be transferred over the network in an HTTP response. Please ensure that the data returned from onGet() is limited to only strings, numbers, booleans, arrays or objects, and does not have any circular references. Error: Unable to serialize value.
What is the ideal way to load the dynamic markdown content? I could do the following on Astro and it would work flawlessly,
Reproduction
https://github.com/axelthat/qwik-bug-2
Steps to reproduce
npm install npm run dev
And then on browser, goto
/blogs/blog1
or/blogs/blog2
.System Info
Additional Information
No response