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

feat(space-nuxt-base): run tunnel on start up #75

Merged
merged 5 commits into from
Jul 26, 2024

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Jul 25, 2024

What?

This PR runs a tunnel on start up, if you have the TUNNEL_COMMAND environment variable.

For example,

TUNNEL_COMMAND="ngrok http --region=eu --hostname=eunjae.eu.ngrok.io 3000"

If not, nothing changes.

Why?

This helps you care less about running a tunnel when working on space / tool plugins. The tunnel will quit as well when you quit the dev server.

Implementation Details

close hook

I've tried this in order to quit the tunnel.

nuxt.hook("close", () => { ... });

However, ctrl + c just quit the Nuxt app without triggering the close hook.

Screenshot 2024-07-25 at 15 26 19

And it's because it's called when "gracefully closing".

So close hook is not useful in our case.

closing

If you stop the dev server with ctrl + c, the dev server process is killed, and that tunnel command is a child process of the dev server process. So it's automatically killed as well. So we don't need to take care of killing the tunnel.

restarting

If you modify nuxt.config.ts file, Nuxt will restart the app. It actually re-starts a process, meaning the previous process is killed, and so is the tunnel process.

So the current implementation should be enough.

export default defineNuxtModule({
setup(_options, nuxt) {
const tunnelCommand = process.env.TUNNEL_COMMAND;
if (process.env.NODE_ENV !== 'development' || !tunnelCommand) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @eunjae-lee this might not be ready yet, I am just leaving comments as I am OOO tomorrow, but not to block you.

nit: process.env.NODE_ENV !== 'development' is this check needed? From what I understood, the listen hook will only be triggered when dev server is loaded. But I might be missing something here.

question: What happens when the dev server is closed, will the tunnel still be running?

Copy link
Contributor Author

@eunjae-lee eunjae-lee Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually added the explanation in the PR description, but if the dev server is closed, the tunnel will be closed as well, because the tunnel is the child process of the dev server process. When the parent dies, the child dies as well. (horrific dev story)

process.env.NODE_ENV !== 'development' -> I guess we won't really need it because listen is for the dev server as you said, however, if we have this, the production build will not include the code within the if clause at all, and it means we won't include execa dependency as well in the bundle output. I actually copied this https://github.com/nuxtus/localtunnel/blob/main/src/module.ts#L44, and I believe that's why they did so?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂😂

Totally missed the description sorry. I read it now, thank you for the details. Everything looks perfect. Can't wait to try it 🫰🏻

@eunjae-lee
Copy link
Contributor Author

eunjae-lee commented Jul 25, 2024

  • TODO for myself: add TUNNEL_COMMAND to .env.example files in all starters

@eunjae-lee eunjae-lee merged commit 4c18b9f into main Jul 26, 2024
1 check passed
@eunjae-lee eunjae-lee deleted the feat/run-tunnel-on-start-up branch July 26, 2024 08:50
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 this pull request may close these issues.

2 participants