-
Notifications
You must be signed in to change notification settings - Fork 157
Feat: Add support for dynamic import of templates #2230
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
base: canary
Are you sure you want to change the base?
Changes from all commits
8a82517
7d2e095
e0a9c00
fc9b159
fce978c
2e4356a
d2a0ce0
136be6b
5dfbb2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| '@faustwp/core': minor | ||
| --- | ||
|
|
||
| Feat: Added support `next/dynamic` imports for templates to reduce initial bundle size in a way that's backwards compatible with static imports. | ||
|
|
||
| This solves a known issue in Faust where all defined templates are bundled together and loaded on every WordPress page. By enabling the use of dynamic importing of templates this issue is resolved. Now templates are only loaded as needed per route. | ||
|
|
||
| It's recommended you migrate to dynamic imports by updating your template file. Here's an example: | ||
|
|
||
| ```js title=src/wp-templates/index.js | ||
| // Old Static Templates | ||
| import category from './category'; | ||
| import tag from './tag'; | ||
| import frontPage from './front-page'; | ||
| import page from './page'; | ||
| import single from './single'; | ||
|
|
||
| export default { | ||
| category, | ||
| tag, | ||
| 'front-page': frontPage, | ||
| page, | ||
| single, | ||
| }; | ||
|
|
||
| // New Dynamic Templates | ||
| import dynamic from 'next/dynamic'; | ||
|
|
||
| const category = dynamic(() => import('./category.js')); | ||
| const tag = dynamic(() => import('./tag.js')); | ||
| const frontPage = dynamic(() => import('./front-page.js')); | ||
| const page = dynamic(() => import('./page.js')); | ||
|
|
||
| // The above examples assume use of default exports. If you are using named exports you'll need to handle that: | ||
| const single = dynamic(() => import('./single.js').then(mod => mod.Single)); | ||
|
|
||
| export default { | ||
| category, | ||
| tag, | ||
| 'front-page': frontPage, | ||
| page, | ||
| single, | ||
| }; | ||
| ``` | ||
|
|
||
| For further info see the Next.js docs on the use of [`next/dynamic`](https://nextjs.org/docs/pages/guides/lazy-loading#nextdynamic-1). |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,58 +1,61 @@ | ||||
| import { gql } from '@apollo/client'; | ||||
| import Image from 'next/image'; | ||||
| import Image from 'next/legacy/image'; | ||||
|
||||
| export default function FeaturedImage({ | ||||
| image, | ||||
| width, | ||||
| height, | ||||
| className, | ||||
| priority, | ||||
| layout, | ||||
| ...props | ||||
| image, | ||||
| width, | ||||
| height, | ||||
| className, | ||||
| priority, | ||||
| layout, | ||||
| ...props | ||||
| }) { | ||||
| const src = image?.sourceUrl; | ||||
|
|
||||
| if (!src) return null; | ||||
|
|
||||
| const { altText = '', mediaDetails = {} } = image ?? {}; | ||||
|
|
||||
| layout = layout ?? 'fill'; | ||||
|
|
||||
| const dimensions = { | ||||
| width: layout === 'fill' ? undefined : width ?? mediaDetails?.width, | ||||
| height: layout === 'fill' ? undefined : height ?? mediaDetails?.height | ||||
| }; | ||||
|
|
||||
| if (layout !== 'fill' && (!dimensions.width || !dimensions.height)) return null; | ||||
|
|
||||
| return ( | ||||
| <figure className={className}> | ||||
| <Image | ||||
| src={src} | ||||
| alt={altText} | ||||
| fill={layout} | ||||
| width={dimensions.width} | ||||
| height={dimensions.height} | ||||
| priority={priority} | ||||
| {...props} | ||||
| /> | ||||
| </figure> | ||||
| ); | ||||
| const src = image?.sourceUrl; | ||||
|
|
||||
| if (!src) return null; | ||||
|
|
||||
| const { altText = '', mediaDetails = {} } = image ?? {}; | ||||
|
|
||||
| layout = layout ?? 'fill'; | ||||
|
|
||||
| const dimensions = { | ||||
| width: layout === 'fill' ? undefined : width ?? mediaDetails?.width, | ||||
| height: layout === 'fill' ? undefined : height ?? mediaDetails?.height, | ||||
| }; | ||||
|
|
||||
| if (layout !== 'fill' && (!dimensions.width || !dimensions.height)) | ||||
| return null; | ||||
|
|
||||
| console.log(layout, mediaDetails, dimensions); | ||||
|
|
||||
|
Comment on lines
+28
to
+29
|
||||
| console.log(layout, mediaDetails, dimensions); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,29 @@ | ||
| import category from './category'; | ||
| import tag from './tag'; | ||
| import frontPage from './front-page'; | ||
| import page from './page'; | ||
| import single from './single'; | ||
| import dynamic from 'next/dynamic'; | ||
|
|
||
| const category = dynamic(() => import('./category.js'), { | ||
| loading: () => <p>Loading Category Template...</p>, | ||
| }); | ||
|
|
||
| const tag = dynamic(() => import('./tag.js'), { | ||
| loading: () => <p>Loading Tag Template...</p>, | ||
| }); | ||
|
|
||
| const frontPage = dynamic(() => import('./front-page.js'), { | ||
| loading: () => <p>Loading Front Page Template...</p>, | ||
| }); | ||
|
|
||
| const page = dynamic(() => import('./page.js'), { | ||
| loading: () => <p>Loading Page Template...</p>, | ||
| }); | ||
|
|
||
| const single = dynamic(() => import('./single.js'), { | ||
| loading: () => <p>Loading Single Post Template...</p>, | ||
| }); | ||
|
|
||
| export default { | ||
| category, | ||
| tag, | ||
| 'front-page': frontPage, | ||
| page, | ||
| single, | ||
| category, | ||
| tag, | ||
| 'front-page': frontPage, | ||
| page, | ||
| single, | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,12 +10,17 @@ import React, { | |||||
| } from 'react'; | ||||||
| import { getApolloAuthClient, getApolloClient } from '../client.js'; | ||||||
| import { getConfig } from '../config/index.js'; | ||||||
| import { getTemplate } from '../getTemplate.js'; | ||||||
| import { | ||||||
| getTemplate, | ||||||
| isDynamicComponent, | ||||||
| loadDynamicComponent, | ||||||
| } from '../getTemplate.js'; | ||||||
| import { useAuth } from '../hooks/useAuth.js'; | ||||||
| import { SEED_QUERY, SeedNode } from '../queries/seedQuery.js'; | ||||||
| import { FaustContext, FaustQueries } from '../store/FaustContext.js'; | ||||||
| import { getQueryParam } from '../utils/convert.js'; | ||||||
| import { isWordPressPreview } from '../utils/isWordPressPreview.js'; | ||||||
| import type { WordPressTemplate as WordPressTemplateType } from '../getWordPressProps.js'; | ||||||
|
|
||||||
| export type FaustProps = { | ||||||
| __SEED_NODE__?: SeedNode | null; | ||||||
|
|
@@ -38,6 +43,14 @@ export type FaustTemplateProps<Data, Props = Record<string, never>> = Props & { | |||||
| __TEMPLATE_VARIABLES__?: { [key: string]: any }; | ||||||
| }; | ||||||
|
|
||||||
| function checkDuplicateQueryQueries(template: WordPressTemplateType): void { | ||||||
| if (template.query && template.queries) { | ||||||
| throw new Error( | ||||||
| '`Only either `Component.query` or `Component.queries` can be provided, but not both.', | ||||||
|
||||||
| '`Only either `Component.query` or `Component.queries` can be provided, but not both.', | |
| 'Only either `Component.query` or `Component.queries` can be provided, but not both.', |
Copilot
AI
Feb 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The async operations in these useEffect hooks lack error handling. If loadDynamicComponent fails (e.g., network error, file not found, syntax error in the template), the error will be silently swallowed due to the void operator. Consider adding try-catch blocks to handle errors gracefully, log them, or show an error state to the user.
Copilot
AI
Feb 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dynamic component is loaded twice in two separate useEffect hooks (lines 91-93 and 155-157). This means the same component module could be loaded multiple times unnecessarily. Consider loading it once in a shared way, storing it in state, and reusing it in both useEffects to avoid redundant network requests and improve performance.
Copilot
AI
Feb 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The async operations in this useEffect hook lack error handling. If loadDynamicComponent fails (e.g., network error, file not found, syntax error in the template), the error will be silently swallowed due to the void operator. Consider adding try-catch blocks to handle errors gracefully, log them, or show an error state to the user.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -146,10 +146,29 @@ export function getPossibleTemplates(node: SeedNode) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return possibleTemplates; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type DynamicComponent<C> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preload: () => Promise<{ default: C }>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| displayName?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function isDynamicComponent<C>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| component: C | DynamicComponent<C>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): component is DynamicComponent<C> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (component as DynamicComponent<C>).render?.preload !== undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function loadDynamicComponent<C>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| component: DynamicComponent<C>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<C> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return component.render.preload().then((mod) => mod.default); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+159
to
+165
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (component as DynamicComponent<C>).render?.preload !== undefined; | |
| } | |
| export function loadDynamicComponent<C>( | |
| component: DynamicComponent<C>, | |
| ): Promise<C> { | |
| return component.render.preload().then((mod) => mod.default); | |
| const candidate = component as DynamicComponent<C> | null | undefined; | |
| return ( | |
| typeof candidate === 'object' && | |
| candidate !== null && | |
| typeof (candidate as DynamicComponent<C>).render === 'object' && | |
| (candidate as DynamicComponent<C>).render !== null && | |
| typeof (candidate as DynamicComponent<C>).render.preload === 'function' | |
| ); | |
| } | |
| export function loadDynamicComponent<C>( | |
| component: DynamicComponent<C>, | |
| ): Promise<C> { | |
| const preload = component?.render?.preload; | |
| if (typeof preload !== 'function') { | |
| return Promise.reject( | |
| new Error('loadDynamicComponent was called with a component that does not have a valid render.preload function.'), | |
| ); | |
| } | |
| return Promise.resolve(preload()).then((mod) => mod.default); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation example imports
categorybut then exportssinglewhich is not defined. This should either import and definesingleor export the definedcategoryvariable.