-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from BKWLD/fix-next-app-router
Fix next app router support
- Loading branch information
Showing
4 changed files
with
85 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,64 @@ | ||
import type { ReactElement } from 'react' | ||
import type { LazyVideoProps } from '../types/lazyVideoTypes' | ||
import LazyVideoClient from './LazyVideoClient' | ||
|
||
// This wrapper function exists to take Function props and make them | ||
// serializable for the LazyVideoClient component, which is a Next.js style | ||
// client component. | ||
export default function LazyVideo( | ||
props: LazyVideoProps | ||
): ReactElement | undefined { | ||
|
||
// Destructure some props | ||
const { | ||
src, | ||
sourceMedia, | ||
videoLoader, | ||
} = props | ||
|
||
// Multiple media queries and a loader func are necessary for responsive | ||
const useResponsiveSource = sourceMedia | ||
&& sourceMedia?.length > 1 | ||
&& !!videoLoader | ||
|
||
// Vars that will be conditionally populated | ||
let srcUrl, mediaSrcs | ||
|
||
// Prepare a hash of source URLs and their media query constraint in the | ||
// style expected by useMediaQueries. | ||
if (useResponsiveSource) { | ||
const mediaSrcEntries = sourceMedia.map(media => { | ||
const url = videoLoader({ src, media }) | ||
return [url, media] | ||
}) | ||
// If the array ended up empty, abort | ||
if (mediaSrcEntries.filter(([url]) => !!url).length == 0) return | ||
|
||
// Make the hash | ||
mediaSrcs = Object.fromEntries(mediaSrcEntries) | ||
|
||
// Make a simple string src url | ||
} else { | ||
if (videoLoader) srcUrl = videoLoader({ src }) | ||
else if (typeof src == 'string') srcUrl = src | ||
if (!srcUrl) return // If no url could be built, abort | ||
} | ||
|
||
// Render client component | ||
return ( | ||
<LazyVideoClient {...{ | ||
...props, | ||
|
||
// Remove client-unfriendly props | ||
videoLoader: undefined, | ||
src: undefined, | ||
sourceMedia: undefined, | ||
|
||
// Add client-friendly props | ||
srcUrl, | ||
mediaSrcs, | ||
}} | ||
/> | ||
) | ||
} | ||
|
This file contains 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,3 @@ | ||
// Export the server component as the defalut LazyVideo component | ||
import LazyVideoServer from './LazyVideoServer' | ||
export default LazyVideoServer |
This file contains 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