hmsk / frontmatter-markdown-loader

📝 Webpack Loader for: FrontMatter (.md) -> HTML + Attributes (+ React/Vue Component)
https://hmsk.github.io/frontmatter-markdown-loader/
MIT License
255 stars 18 forks source link

Render and staticRenderFns in SSR #96

Closed jefrydco closed 4 years ago

jefrydco commented 4 years ago

In version 1.8.0, we can set the render and staticRenderFns to data via asyncData since it was a string, but in the current version, I think we can't do that. Is there any workaround for that?

hmsk commented 4 years ago

Right. https://github.com/hmsk/frontmatter-markdown-loader/issues/83 may answers your question.

jefrydco commented 4 years ago

Sure, that answers my question, but the content of markdown won't render from the server. Please check out this https://github.com/jefrydco/jefrydco/tree/feat/frontmatter-markdown-loader

hmsk commented 4 years ago

Looks abusing asynchronous import which couldn't be resolved on the server-side 👀 <component :is=""> expects function which returns Promise by import for SSR + asynchronous importing. Or just a ready-made component object synchronously. Your approach is passing an object which will be fulfilled by another asynchronous process, but <component> can't wait on the server-side.

Here's the quick patch to fix the problem with synchronous way:

diff --git a/pages/blog/_slug/index.vue b/pages/blog/_slug/index.vue
index 25f5bd4..7393e4a 100644
--- a/pages/blog/_slug/index.vue
+++ b/pages/blog/_slug/index.vue
@@ -295,7 +295,7 @@ export default {
       blog: null
     }
   },
-  async created() {
+  created() {
     const slug = this.$route && this.$route.params && this.$route.params.slug
     const locale = this.$i18n.locale
     const locales = this.$i18n.locales
@@ -306,34 +306,33 @@ export default {
     try {
       if (locale === defaultLocale) {
         editPath = `contents/blogs/${slug}/index.md`
-        blog = await import(`~/contents/blogs/${slug}/index.md`)
+        blog = require(`~/contents/blogs/${slug}/index.md`)
       } else {
         editPath = `contents/blogs/${slug}/index.${locale}.md`
-        blog = await import(`~/contents/blogs/${slug}/index.${locale}.md`)
+        blog = require(`~/contents/blogs/${slug}/index.${locale}.md`)
       }

-      await (() => {
-        if (blog !== null && typeof blog !== 'undefined') {
-          const fullPath = `https://jefrydco.id/blog/${blog.attributes.slug}`
-          this.availableLocales = availableLocales
-          this.blog = {
-            img: blog.attributes.img,
-            imgCreator: blog.attributes.imgCreator,
-            title: blog.attributes.title,
-            description: blog.attributes.description,
-            postedDate: blog.attributes.postedDate,
-            updatedDate: blog.attributes.updatedDate,
-            slug: blog.attributes.slug,
-            readingTime: readingTime(blog.html),
-            component: blog.vue.component,
-            fullPath,
-            discussLink: `https://twitter.com/search?q=${encodeURIComponent(
-              fullPath
-            )}`,
-            editLink: `https://github.com/jefrydco/jefrydco/edit/master/${editPath}`
-          }
+      if (blog !== null && typeof blog !== 'undefined') {
+        const fullPath = `https://jefrydco.id/blog/${blog.attributes.slug}`
+        this.availableLocales = availableLocales
+        this.blog = {
+          img: blog.attributes.img,
+          imgCreator: blog.attributes.imgCreator,
+          title: blog.attributes.title,
+          description: blog.attributes.description,
+          postedDate: blog.attributes.postedDate,
+          updatedDate: blog.attributes.updatedDate,
+          slug: blog.attributes.slug,
+          readingTime: readingTime(blog.html),
+          component: blog.vue.component,
+          fullPath,
+          discussLink: `https://twitter.com/search?q=${encodeURIComponent(
+            fullPath
+          )}`,
+          editLink: `https://github.com/jefrydco/jefrydco/edit/master/${editPath}`
         }
-      })()
+      }
     } catch (error) {
       this.$router.replace('/blog')
     }
jefrydco commented 4 years ago

Sorry for late reply, and thanks for the patch. It really worked! I also have created PR for example mentioned in https://github.com/hmsk/frontmatter-markdown-loader/issues/83.