wonder95's avatar

Unable to access Breeze/Vue site at ngrok URL

I'm trying to set up ngrok using valet share for OAuth testing purposes in my Breeze app (using Vue), but I am unable to access my site at the ngrok URL. (the normal valet URL is http://members.test`) I have done these steps:

  • Run valet share to get my ngrok URL
  • Run valet secure members
  • Set APP_URL to the ngrok URL in .env
  • Set SESSION_DOMAIN to just the domain (without the https://)
  • Set ASSET_URL to my ngrok url
  • Added the tag to app.blade.php
  • cleared cache and config

When I attempt to navigate to the ngrok URL, it redirects me to the valet url with https - https://members.test, and I get the vite console errors when I try to log in.

[vite] failed to connect to websocket.
your current setup:
  (browser) [::1]:5173/ <--[HTTP]--> localhost:5173/ (server)
  (browser) [::1]:5173/ <--[WebSocket (failing)]--> localhost:5173/ (server)
Check out your Vite / network configuration and https://vitejs.dev/config/server-options.html#server-hmr .

I don't have the members.test defined anywhere in my env file. What else do I need to do to be able to access my site at the ngrok url?

0 likes
4 replies
LaryAI's avatar
Level 58

It sounds like you're encountering issues with Vite's Hot Module Replacement (HMR) when trying to use ngrok to expose your local Laravel Breeze/Vue application. The problem is likely due to Vite's default behavior where it tries to connect to the local server directly for HMR, which doesn't work through ngrok because the domain changes.

Here are the steps to configure your environment to work correctly with ngrok:

  1. Update Vite Configuration: You need to tell Vite to use the ngrok URL for HMR. This can be done by modifying the vite.config.js file in your project. You should specify the server.hmr option with your ngrok URL.

    Here's how you can adjust the Vite configuration:

    import { defineConfig } from 'vite';
    import vue from '@vitejs/plugin-vue';
    
    // Replace 'your-ngrok-url' with your actual ngrok URL
    const ngrokUrl = 'https://your-ngrok-url';
    
    export default defineConfig({
      plugins: [vue()],
      server: {
        hmr: {
          host: ngrokUrl.replace('https://', ''),
          protocol: 'wss',
          clientPort: 443
        }
      }
    });
    

    Make sure to replace 'https://your-ngrok-url' with the actual HTTPS URL provided by ngrok.

  2. Ensure Correct Environment Variables: You've already set APP_URL to the ngrok URL in your .env file, which is good. Make sure that SESSION_DOMAIN and ASSET_URL are also correctly set. SESSION_DOMAIN should be the domain part of your ngrok URL without the protocol (e.g., your-ngrok-url).

  3. Clear Laravel Caches: After making changes to your .env file or any configuration, ensure you clear the Laravel cache. You can do this by running the following commands in your terminal:

    php artisan config:clear
    php artisan cache:clear
    
  4. Restart Valet and ngrok: After making these changes, restart Valet and ngrok to ensure all settings are correctly applied:

    valet restart
    valet share
    
  5. Check Browser Console: Once you've made these changes and accessed your site via the ngrok URL, check the browser console for any errors. If there are issues related to HMR, the changes to vite.config.js should resolve them.

By following these steps, you should be able to access your Laravel Breeze/Vue application through ngrok without issues, and Vite's HMR should function correctly over the ngrok tunnel.

wonder95's avatar

ok, the only thing that doesn't make sense about this is that running valet share is what gives you the ngrok URL, so you're supposed to make the changes to add the URL to the config file, and then run valet share again?

martinbean's avatar

@wonder95 No. If you’re using ngrok, you will get a URL that ends with ngrok.io I think it is. That’s the URL, which maps to port 80 of your application, that you want to use.

You’ll also need to configure trusted proxies so that your application correctly detects it’s running via ngrok’s proxy, and therefore uses the correct hostname when generating URLs.

wonder95's avatar

Hmmm, ok. So I just put in the ngrok URL in the

$middleware->trustProxies(at: []);

?

Please or to participate in this conversation.