0

I'm building a desktop app using tauri with Next.js and when I try to create a new window, on first load it fails, but if I comment and uncomment the code it works perfectly.

As it's on the first load, I can't even compile the build.

Here's the portion of the code to open a new window:

  const openWindow = async (e) => {
    const table = JSON.stringify({
      host: active.host,
      port: active.port,
      user: active.user,
      password: active.password,
      database: e,
    });
    try {
      await new WebviewWindow("test", {
        url: "/database?data=" + table,
      });
    } catch (e) {
      console.log("error", e);
    }
  };

And here's where I invoke que function to open the window:

{loading === false
            ? databases.map((database) => {
                return (
                  <Tr>
                    <Td>
                      <Button
                        onClick={() => openWindow(database.dbname)}
                        variant={"link"}
                      >
                        {database.dbname}
                      </Button>
                    </Td>

                    <Td isNumeric>{database.tablecnt}</Td>
                    <Td isNumeric>{database.indexcnt}</Td>
                    <Td isNumeric>{database.viewcnt}</Td>
                    <Td isNumeric>{database.proccnt}</Td>
                  </Tr>
                );
              })
            : null}

Here's the error I get:

error - ReferenceError: window is not defined
    at file:///D:/proyectos/sql-defrag/node_modules/@tauri-apps/api/chunk-QSWLDHGO.js:1:9678
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15) {
  page: '/'
}

If anyone has any clue I'd appreciate some help.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • ```export default async function openNewWindow() { try { const WebviewWindow = await ( await import("@tauri-apps/api/window") ).WebviewWindow; new WebviewWindow("theUniqueLabel", { url: "path/to/page.html", }); } catch (error) { console.log(error); } } ``` – GusiiSenpaii Jan 30 '23 at 16:22
  • Does this answer your question? [Window is not defined in Next.js React app](https://stackoverflow.com/questions/55151041/window-is-not-defined-in-next-js-react-app) – juliomalves May 20 '23 at 18:38

0 Answers0