mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
## App Router Moves the `with-mux-video` example to idiomatic App Router, with goodies like * server component data fetching * server actions * layouts * route groups * loading UI ## Mux Dependencies * @mux/mux-node 7 -> 8 * @mux/mux-player-react 1 -> 2 * @mux/upchunk + custom UI -> @mux/mux-uploader ## In other news... * Fleshed out the README * Updated imagery * Moved from styled jsx to tailwind and lightly updated styles ## Contributor Checklist * [x] The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md * [x] Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md --------- Co-authored-by: Sam Ko <[email protected]>
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import Mux from "@mux/mux-node";
|
|
import { redirect } from "next/navigation";
|
|
import { Status } from "./types";
|
|
import AssetStatusPoll from "./AssetStatusPoll";
|
|
|
|
// reads MUX_TOKEN_ID and MUX_TOKEN_SECRET from your environment
|
|
const mux = new Mux();
|
|
|
|
const checkAssetStatus = async (assetId: string): Promise<Status> => {
|
|
const asset = await mux.video.assets.retrieve(assetId);
|
|
|
|
// if the asset is ready and it has a public playback ID,
|
|
// (which it should, considering the upload settings we used)
|
|
// redirect to its playback page
|
|
if (asset.status === "ready") {
|
|
const playbackIds = asset.playback_ids;
|
|
if (Array.isArray(playbackIds)) {
|
|
const playbackId = playbackIds.find((id) => id.policy === "public");
|
|
if (playbackId) {
|
|
redirect(`/v/${playbackId.id}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
status: asset.status,
|
|
errors: asset.errors,
|
|
};
|
|
};
|
|
|
|
// For better performance, we could cache and use a Mux webhook to invalidate the cache.
|
|
// https://docs.mux.com/guides/listen-for-webhooks
|
|
// For this example, calling the Mux API on each request and then polling is sufficient.
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export default async function Page({
|
|
params: { assetId },
|
|
}: {
|
|
params: { assetId: string };
|
|
}) {
|
|
const initialStatus = await checkAssetStatus(assetId);
|
|
return (
|
|
<AssetStatusPoll
|
|
initialStatus={initialStatus}
|
|
checkAssetStatus={async () => {
|
|
"use server";
|
|
return await checkAssetStatus(assetId);
|
|
}}
|
|
/>
|
|
);
|
|
}
|