Skip to content

Commit

Permalink
fix(live): pass correct URL and DEPLOY_URL env vars to plugins (#…
Browse files Browse the repository at this point in the history
…5607)

* fix(live): make LIVE_URL env var available in --live mode

Before this fix there was no way to access the live URL from functions
or build plugins.

* fix(live): pass URL to build env instead of setting LIVE_URL

turns out that it's only builds and build plugins that have this problem
and that functions and edge functions already pick up the right URL and DEPLOY_URL
in the environment. This fixes the URL for builds and build plugins when --live is
set, but the problem still remains when not running in dev mode.

* fix(live): make the fix work for netlify dev without live as well

Now the dev server URL is always correctly set as URL and DEPLOY_URL for builds and
build plugins in dev mode

* fix(live): remove redundant log message

* chore: fix linting issues

---------

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
  • Loading branch information
biilmann and eduardoboucas authored Apr 12, 2023
1 parent 80b575e commit 160e8a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/commands/dev/dev.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { startNetlifyGraph, startPollingForAPIAuthentication } from '../../utils
import { startLiveTunnel } from '../../utils/live-tunnel.mjs'
import openBrowser from '../../utils/open-browser.mjs'
import { generateInspectSettings, startProxyServer } from '../../utils/proxy-server.mjs'
import { getProxyUrl } from '../../utils/proxy.mjs'
import { runDevTimeline } from '../../utils/run-build.mjs'
import { getGeoCountryArgParser } from '../../utils/validation.mjs'

Expand Down Expand Up @@ -124,9 +125,23 @@ const dev = async (options, command) => {
startPollingForAPIAuthentication({ api, command, config, site, siteInfo })
}

const liveTunnelUrl = await handleLiveTunnel({ options, site, api, settings })
const url = liveTunnelUrl || getProxyUrl(settings)
process.env.URL = url
process.env.DEPLOY_URL = url

log(`${NETLIFYDEVWARN} Setting up local development server`)

const { configPath: configPathOverride } = await runDevTimeline({ cachedConfig, options, settings, site })
const { configPath: configPathOverride } = await runDevTimeline({
cachedConfig,
options,
settings,
site,
env: {
URL: url,
DEPLOY_URL: url,
},
})

await startFunctionsServer({
api,
Expand Down Expand Up @@ -159,7 +174,7 @@ const dev = async (options, command) => {

const inspectSettings = generateInspectSettings(options.edgeInspect, options.edgeInspectBrk)

let url = await startProxyServer({
await startProxyServer({
addonsUrls,
config,
configPath: configPathOverride,
Expand All @@ -176,16 +191,10 @@ const dev = async (options, command) => {
state,
})

const liveTunnelUrl = await handleLiveTunnel({ options, site, api, settings })
url = liveTunnelUrl || url

if (devConfig.autoLaunch !== false) {
await openBrowser({ url, silentBrowserNoneError: true })
}

process.env.URL = url
process.env.DEPLOY_URL = url

await startNetlifyGraph({
command,
config,
Expand Down
8 changes: 6 additions & 2 deletions src/utils/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@ const onRequest = async (
proxy.web(req, res, options)
}

export const getProxyUrl = function (settings) {
const scheme = settings.https ? 'https' : 'http'
return `${scheme}://localhost:${settings.port}`
}

export const startProxy = async function ({
addonsUrls,
config,
Expand Down Expand Up @@ -665,8 +670,7 @@ export const startProxy = async function ({

await Promise.all(eventQueue)

const scheme = settings.https ? 'https' : 'http'
return `${scheme}://localhost:${settings.port}`
return getProxyUrl(settings)
}

const BYTES_LIMIT = 30
3 changes: 2 additions & 1 deletion src/utils/run-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const getBuildOptions = ({
saveConfig,
})

const runNetlifyBuild = async ({ cachedConfig, options, settings, site, timeline = 'build' }) => {
const runNetlifyBuild = async ({ cachedConfig, env, options, settings, site, timeline = 'build' }) => {
const { default: buildSite, startDev } = await netlifyBuildPromise
const sharedOptions = getBuildOptions({
cachedConfig,
Expand Down Expand Up @@ -112,6 +112,7 @@ const runNetlifyBuild = async ({ cachedConfig, options, settings, site, timeline
// Set `quiet` to suppress non-essential output from Netlify Build unless
// the `debug` flag is set.
quiet: !options.debug,
env,
}

// Run Netlify Build using the `startDev` entry point.
Expand Down

1 comment on commit 160e8a0

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

  • Package size: 306 MB

Please sign in to comment.