Skip to content
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

SubtitlesOctopus Nextjs Runtime Error #157

Closed
Abdullah-988 opened this issue Apr 16, 2023 · 4 comments
Closed

SubtitlesOctopus Nextjs Runtime Error #157

Abdullah-988 opened this issue Apr 16, 2023 · 4 comments

Comments

@Abdullah-988
Copy link

I've been trying to solve this problem since forever
when using SubtitlesOctopus with Nextjs project I always get errors like

image

I'm using Nextjs With Typescript
My code:

// Player.tsx

import { useEffect, useRef } from "react";
// @ts-ignore
import SubtitlesOctopus from "libass-wasm";

const MainDomain = "https://cdn..../"; // Basically my CDN url

type PlayerProps = {
  eng: string;
};

export default function Player({ eng }: PlayerProps) {

 useEffect(() => {
    Subtitles();
  }, []);

  function Subtitles() {
    var options = {
      video: document.querySelector("video"),
      subUrl: MainDomain + eng,
      workerUrl: new URL("libass-wasm/dist/js/subtitles-octopus-worker.js", import.meta.url)
    };

    var instance = new SubtitlesOctopus(options);
  }

  return <video />;
}

I would be appreciated If someone know how to solve this issue or If someone can make a repo example of how to use SubtitlesOctopus with Nextjs Typescript

Thank You!

@zoriya
Copy link
Contributor

zoriya commented Apr 18, 2023

Subtitle octopus does not provide typescript definitions (it could if #95 is merged one day). To include types, you must create a .d.ts file that define the type of SubtitleOctopus, you can refer to this example if you want.

I created this file by copy-pasting the type.d.ts file from the PR and adding necessary lines to make it work from another project.

@Abdullah-988
Copy link
Author

Same Error

image

The error is probably from the worker when I comment the worker option

function Subtitles() {
    var options: any = {
      video: document.querySelector("video"),
      subUrl: MainDomain + eng,
      // workerUrl: new URL("libass-wasm/dist/js/subtitles-octopus-worker.js", import.meta.url),
    };

    var instance = new SubtitlesOctopus(options);
  }

this shows:
image

@jeffuofa93
Copy link

Did you ever find a fix for this @Abdullah-988? Running into the same issue

@Abdullah-988
Copy link
Author

Abdullah-988 commented May 2, 2023

@jeffuofa93

It seems that the issue was workerUrl and its options

I fixed the issue by copying the dist folder from libass-wasm/dist/js to .next build and development folder using copy-webpack-plugin plugin in webpack

make sure you install copy-webpack-plugin package and
In next.config.js file:

/** @type {import('next').NextConfig} */
const CopyWebpackPlugin = require("copy-webpack-plugin");

const nextConfig = {
  webpack: (config, { isServer }) => {
    config.plugins.push(
      new CopyWebpackPlugin({
        patterns: [
          {
            from: "node_modules/libass-wasm/dist/js",
            to: `${isServer ? "../" : ""}static`,
          },
        ],
      })
    );

    return config;
  },
  reactStrictMode: true,
};

module.exports = nextConfig;

in the function:

var instance = new SubtitlesOctopus({
    video: video,
    subUrl: MainDomain + Source,
    useLocalFonts: false,
    prescaleFactor: 3,
    availableFonts: {
      Font: "https://yourfonturl.com"
    },
    fallbackFont: "https://yourfonturl.com",
    fonts: [ "https://yourfonturl.com" ],
    workerUrl: "/_next/static/subtitles-octopus-worker.js" // <---------

    // or

    workerUrl: "/static/subtitles-octopus-worker.js"
  });

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants