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

proposal: use dynamic imports for routes #1405

Open
iuioiua opened this issue Jul 4, 2023 · 7 comments · May be fixed by #2294
Open

proposal: use dynamic imports for routes #1405

iuioiua opened this issue Jul 4, 2023 · 7 comments · May be fixed by #2294
Assignees

Comments

@iuioiua
Copy link
Collaborator

iuioiua commented Jul 4, 2023

Deno Deploy now supports statically analyzable dynamic imports. This may remove, if not reduce, reliance on the fresh.gen.ts file.

@marvinhagemeister
Copy link
Collaborator

I'm not sure how it would allow us to get rid of the manifest. Would love to learn more as I'd like to get rid of it too if possible.

@lionel-rowe
Copy link
Contributor

I don't think this could eliminate fresh.gen.ts as the static file names still have to be hard-coded, but it could at least make for cleaner git diffs, as you wouldn't have to store each import in a dynamic $n variable that gets re-numbered any time a file with a lexicographically earlier name is added. Something like this:

- import * as $0 from "./routes/index.tsx";
- import * as $1 from "./routes/passthrough.tsx";
- import * as $$0 from "./islands/Counter.tsx";
- import * as $$1 from "./islands/Passthrough.tsx";

  const manifest = {
    routes: {
-     "./routes/index.tsx": $0,
+     "./routes/index.tsx": () => import("./routes/index.tsx"),
-     "./routes/passthrough.tsx": $1,
+     "./routes/passthrough.tsx": () => import("./routes/passthrough.tsx"),
    },
    islands: {
-     "./islands/Counter.tsx": $$0,
+     "./islands/Counter.tsx": () => import("./islands/Counter.tsx"),
-     "./islands/Passthrough.tsx": $$1,
+     "./islands/Passthrough.tsx": () => import("./islands/Passthrough.tsx"),
    },
    baseUrl: import.meta.url,
  };

@iuioiua
Copy link
Collaborator Author

iuioiua commented Oct 10, 2023

Ah, sorry for not responding sooner. Yes, after creating this issue, I realised I was wrong. Though, @lionel-rowe, your idea looks like it has potential! I'd happily look at a PR if you wanted to submit one. @marvinhagemeister, do you see any issues with the proposal?

@iuioiua
Copy link
Collaborator Author

iuioiua commented Oct 10, 2023

Another idea:

- import * as $0 from "./routes/index.tsx";
- import * as $1 from "./routes/passthrough.tsx";
- import * as $$0 from "./islands/Counter.tsx";
- import * as $$1 from "./islands/Passthrough.tsx";

  const manifest = {
    routes: {
-     "./routes/index.tsx": $0,
+     "./routes/index.tsx": await import("./routes/index.tsx"),
-     "./routes/passthrough.tsx": $1,
+     "./routes/passthrough.tsx": await import("./routes/passthrough.tsx"),
    },
    islands: {
-     "./islands/Counter.tsx": $$0,
+     "./islands/Counter.tsx": await import("./islands/Counter.tsx"),
-     "./islands/Passthrough.tsx": $$1,
+     "./islands/Passthrough.tsx": await import("./islands/Passthrough.tsx"),
    },
    baseUrl: import.meta.url,
  };

@iuioiua
Copy link
Collaborator Author

iuioiua commented Oct 10, 2023

Actually, I have some time. I'll look into this.

@lionel-rowe
Copy link
Contributor

@iuioiua is there a reason the deferred () => import(...) version wouldn't work? Obviously it'd necessitate changes in the calling code, and maybe it wouldn't play nicely with sync plugins etc, but the advantage if it could be made to work is that you'd get code splitting for free.

@csvn
Copy link
Contributor

csvn commented Nov 29, 2023

Wouldn't it be a bit slower to do await import(...) in serial like that? It would be one FS operation at a time in serial instead of doing all of them in parallell. The deferred option of doing () => import(...) would not necessitate the loading of all routes in serial when loading fresh.gen.ts.

@deer deer linked a pull request Jan 31, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

4 participants