Skip to content

Commit

Permalink
fix: async function as next config (#252)
Browse files Browse the repository at this point in the history
fix #251
  • Loading branch information
luwes authored May 5, 2024
1 parent ba41a27 commit a8db824
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion examples/default-provider/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { withNextVideo } from 'next-video/process';
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withNextVideo(nextConfig);
export default async function config() {
return withNextVideo(nextConfig);
}

// Amazon S3 example
// export default withNextVideo(nextConfig, {
Expand Down
16 changes: 12 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { env, cwd } from 'node:process';
import { cwd } from 'node:process';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import nextConfig from 'next/config.js'
import nextConfig from 'next/config.js';
import type { NextConfig } from 'next';
// @ts-ignore
const getConfig = nextConfig.default;

Expand Down Expand Up @@ -68,7 +69,7 @@ export const videoConfigDefault: VideoConfigComplete = {
* The video config is then stored as an environment variable __NEXT_VIDEO_OPTS.
*/
export async function getVideoConfig(): Promise<VideoConfigComplete> {
let nextConfig = getConfig();
let nextConfig: NextConfig | undefined = getConfig();

if (!nextConfig?.serverRuntimeConfig?.nextVideo) {
try {
Expand All @@ -88,5 +89,12 @@ export async function getVideoConfig(): Promise<VideoConfigComplete> {
async function importConfig(file: string) {
const absFilePath = path.resolve(cwd(), file);
const fileUrl = pathToFileURL(absFilePath).href;
return (await import(/* webpackIgnore: true */ fileUrl))?.default;

const mod = await import(/* webpackIgnore: true */ fileUrl);
const config: (() => NextConfig) | NextConfig | undefined = mod?.default;

if (typeof config === 'function') {
return config();
}
return config;
}

0 comments on commit a8db824

Please sign in to comment.