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

fix: fix cache error with geo country mock #4821

Merged
merged 15 commits into from
Jul 20, 2022
Merged
2 changes: 1 addition & 1 deletion docs/commands/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ netlify dev
**Flags**

- `command` (*string*) - command to run
- `country` (*string*) - Two-letter country code (ISO 3166-1 alpha-2, https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) to use as mock geolocation (enables --geo=mock autmatically)
- `country` (*string*) - Two-letter country code (ISO 3166-1 alpha-2, https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) to use as mock geolocation (enables --geo=mock automatically)
- `dir` (*string*) - dir with static files
- `edgeInspect` (*string*) - enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format
- `edgeInspectBrk` (*string*) - enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ const createDevCommand = (program) => {
.addOption(
new Option(
'--country <geoCountry>',
'Two-letter country code (ISO 3166-1 alpha-2, https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) to use as mock geolocation (enables --geo=mock autmatically)',
'Two-letter country code (ISO 3166-1 alpha-2, https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) to use as mock geolocation (enables --geo=mock automatically)',
).argParser(validateGeoCountryCode),
)
.addOption(
Expand Down
5 changes: 4 additions & 1 deletion src/lib/geo-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const mockLocation = {
subdivision: { code: 'CA', name: 'California' },
}

const isCacheEligible = (cacheObj, mode, country) =>
cacheObj !== undefined && mode === 'cache' && (cacheObj.data.country.code === country || !country)
Copy link
Member

Choose a reason for hiding this comment

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

If you run --country=XX, shouldn't mode become mock? That's what the docs say is happening, and I wonder if reflecting that in the code would make this simpler, because then you wouldn't need to change this check at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we change the mode to mock, I'll still have to change the conditional in order to use the cached location in the case of a match though, right? To something like this?

  const cacheObject = state.get(STATE_GEO_PROPERTY)
  if (geoCountry) mode = 'mock'

  // If we have cached geolocation data and the `--geo` option is set to
  // `cache`, let's try to use it.
  if (cacheObject !== undefined && (mode === 'cache' || cacheObject.data.country.code === geoCountry)) {
    const age = Date.now() - cacheObject.timestamp


/**
* Returns geolocation data from a remote API, the local cache, or a mock
* location, depending on the mode selected.
Expand All @@ -43,7 +46,7 @@ const getGeoLocation = async ({ geoCountry, mode, offline, state }) => {

// If we have cached geolocation data and the `--geo` option is set to
// `cache`, let's try to use it.
if (cacheObject !== undefined && mode === 'cache') {
if (isCacheEligible(cacheObject, mode, geoCountry)) {
const age = Date.now() - cacheObject.timestamp

// Let's use the cached data if it's not older than the TTL. Also, if the
Expand Down