-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a82517
initial implementation of handling for dynamic imports
moonmeister 7d2e095
fix: example image issue
moonmeister e0a9c00
fix: add missing dynamic handling for queries and resolve ts errors.
moonmeister fc9b159
fix: type errors in wordpress template
moonmeister fce978c
fix: type errors in geWPProps
moonmeister 2e4356a
formatting
moonmeister d2a0ce0
fix: name overlap
moonmeister 136be6b
Merge remote-tracking branch 'origin/canary' into dynamic-templates
ahuseyn 5dfbb2e
Update .changeset/funny-ravens-run.md
josephfusco b2c17a7
Update packages/faustwp-core/src/getWordPressProps.tsx
ahuseyn 0683dd9
Update packages/faustwp-core/src/components/WordPressTemplate.tsx
ahuseyn 92f9028
Update docs/how-to/basic-setup/index.md
ahuseyn 0e1ed3a
Update examples/next/faustwp-getting-started/components/FeaturedImage…
ahuseyn 91c22d1
revert legacy Image usage in FeaturedImage
ahuseyn 14a144b
add test coverage for isDynamicComponent and loadDynamicComponent
ahuseyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 52 additions & 50 deletions
102
examples/next/faustwp-getting-started/components/FeaturedImage/FeaturedImage.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,60 @@ | ||
| import { gql } from '@apollo/client'; | ||
| import Image from 'next/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; | ||
|
|
||
| return ( | ||
| <figure className={className}> | ||
| <Image | ||
| src={src} | ||
| alt={altText} | ||
| layout={layout} | ||
| width={dimensions.width} | ||
| height={dimensions.height} | ||
| priority={priority} | ||
| {...props} | ||
| /> | ||
| </figure> | ||
| ); | ||
| } | ||
|
|
||
| FeaturedImage.fragments = { | ||
| entry: gql` | ||
| fragment FeaturedImageFragment on NodeWithFeaturedImage { | ||
| featuredImage { | ||
| node { | ||
| id | ||
| sourceUrl | ||
| altText | ||
| mediaDetails { | ||
| width | ||
| height | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| entry: gql` | ||
| fragment FeaturedImageFragment on NodeWithFeaturedImage { | ||
| featuredImage { | ||
| node { | ||
| id | ||
| sourceUrl | ||
| altText | ||
| mediaDetails { | ||
| width | ||
| height | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| }; |
36 changes: 26 additions & 10 deletions
36
examples/next/faustwp-getting-started/wp-templates/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.