Skip to content

Conversation

@ctjlewis
Copy link
Contributor

@ctjlewis ctjlewis commented Jan 20, 2021

Summary

The CLI's current behavior is to only allow the asterisk wildcard * at the end of a rule. This PR aims to add support for wildcard which matches Netlify's deploy logic, which does not seem to have the same restriction.

  1. See Header config causes CLI to throw "invalid rule: cannot contain anything after *" next-on-netlify#151
  2. Closes Error: invalid rule (A path rule cannot contain anything after * token) #1148

Test plan

Tests were updated in headers.test.js. More robust testing should likely be added before merging.

Changelog

Previously, (*) wildcards had to appear at the end of a rule path, as in /path/to/dir/*. The CLI now supports the asterisk wildcard at any part of the rule, i.e. /test/*/test/*/test.

@ctjlewis ctjlewis requested a review from a team as a code owner January 20, 2021 03:27
@ctjlewis ctjlewis marked this pull request as draft January 20, 2021 03:28
@ctjlewis
Copy link
Contributor Author

If anyone has any suggestions regarding the PR label check, please let me know! I'm not sure what to do to clear that up - I thought the feat: tag was enough 😭

(also cc @lindsaylevine)

@ctjlewis ctjlewis changed the title feat: add support for consistent * wildcard feat(headers): add support for consistent * wildcard Jan 20, 2021
@ctjlewis ctjlewis force-pushed the next-on-netlify-headers branch from a675380 to 7d3ab2d Compare January 20, 2021 04:34
@lindsaylevine lindsaylevine added type: bug code to address defects in shipped code type: feature code contributing to the implementation of a feature and/or user facing functionality labels Jan 20, 2021
@lindsaylevine
Copy link

this looks amazing @ctjlewis !!! the label thing has to be one of the type: ${something} labels 😺 . i added both bug and feat cause i think it qualifies as both, lol. but thanks for being so thorough!! looks like the windows 14.x tests timed out..? 🤔 i'll leave it to @ehmicky and @erezrokah from here :)

@ctjlewis ctjlewis force-pushed the next-on-netlify-headers branch from 7d3ab2d to 5011d32 Compare January 20, 2021 07:52
@erezrokah erezrokah removed the type: feature code contributing to the implementation of a feature and/or user facing functionality label Jan 20, 2021
@ctjlewis ctjlewis changed the title feat(headers): add support for consistent * wildcard fix(headers): add support for consistent * wildcard Jan 20, 2021
@ctjlewis ctjlewis marked this pull request as ready for review January 20, 2021 23:23
@ctjlewis
Copy link
Contributor Author

ctjlewis commented Jan 21, 2021

This will currently match /path/to/dir/:placeholder with /path/to/dir, which we might not want as I understand it (that would effectively be :placeholder matching an empty dir /, wouldn't it?).

This "spec" is not well-worded nor detailed (no offense - I plan to send a PR to clarify the language once this is merged), and it only says:

Paths can contain * or :placeholders. A :placeholder matches anything except /, while a * matches anything.

I definitely knew we should take this to mean / does not match /:placeholder but does match /*, but does the / in the quote provided above refer also to nonexistent directory, i.e., should /path/to/dir match /path/to/dir/:placeholder where it definitely matches /path/to/dir/*?

Without adjusting, /path/to/dir/:placeholder and /path/to/dir/* will both match /path/to/dir, but only the latter will match /path/to/dir/one/two/three.

Once we get clarification on this, this will be ready to finalize.

@ctjlewis ctjlewis marked this pull request as draft January 21, 2021 00:18
@ctjlewis
Copy link
Contributor Author

ctjlewis commented Jan 21, 2021

Just tested: This version of netlify dev correctly handles the next-on-netlify rule /*/_next/static/chunks/*, from netlify/next-on-netlify#151, which inspired this PR.

The dev view finally loads and displays the content!

@erezrokah erezrokah self-requested a review January 21, 2021 16:00
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Hi @ctjlewis, this is great work! Thank for the fix and those comments & tests look awesome 🚀

I'm going to look into our production logic to verify we're not missing anything here before I merge this.

@ctjlewis
Copy link
Contributor Author

Absolutely @erezrokah - if you can share any clarification you find on the production logic, I'd appreciate it. The Custom Headers documentation could use a teeny bit more context IMO (mostly just regarding the /path/to/dir/:placeholder vs /path/to/dir case).

@erezrokah erezrokah force-pushed the next-on-netlify-headers branch from d675c6a to ff78323 Compare January 27, 2021 15:10
@erezrokah
Copy link
Contributor

Hi @ctjlewis sorry for the delay. I added another commit for the headers parsing to make it more consistent with our production logic.
I think it should also make it easier to understand the expected structure.

I'll follow up on the headers matching as well. After finishing the latter I'll open an issue for our docs team to clarify https://docs.netlify.com/routing/headers/#syntax-for-the-headers-file.

@erezrokah
Copy link
Contributor

Once we get clarification on this, this will be ready to finalize.

/path/to/dir/:placeholder should not match /path/to/dir
/path/to/dir/* should match /path/to/dir

I also added some tests for /*test.

The last changes use a regular expression instead of the previous approach - that's much closer to the production logic (it's not pretty though).

@erezrokah erezrokah marked this pull request as ready for review January 28, 2021 12:48
@erezrokah erezrokah force-pushed the next-on-netlify-headers branch from 39b32a1 to b9c85f1 Compare January 28, 2021 14:03
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

@ctjlewis thanks again for helping us to clarify this.

I've opened a follow up issue to our docs team

Copy link
Member

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

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

This looks great — thanks @ctjlewis! 🙌🏻

@erezrokah erezrokah merged commit 5166b97 into netlify:master Jan 29, 2021
@ctjlewis
Copy link
Contributor Author

/path/to/dir/:placeholder should not match /path/to/dir
/path/to/dir/* should match /path/to/dir

Super sorry for the delay in response! One note:

t.assert(matchesPath('/path/to/dir/*/:placeholder', '/path/to/dir/test'))

I wonder if /path/to/dir/*/:placeholder would match /path/to/dir, when it technically... should not?

No idea, not just trying to make more work, just couldn't not notice it lol.

@erezrokah
Copy link
Contributor

erezrokah commented Jan 29, 2021

I wonder if /path/to/dir/*/:placeholder would match /path/to/dir, when it technically... should not?
Another interesting one :)

Well it is matched by our prod logic:
https://github.com/erezrokah/netlify-build-reproductions/blob/7408e60fdaa2d83780c3664474ab813127a6475e/public/_headers#L1
https://6013edd04e27200008943118--erez-build-reproductions.netlify.app/path/to/dir/test/

I'll add that use case to our docs issue, thanks!

Update

I missed read your comment.

  • /path/to/dir/*/:placeholder matches /path/to/dir/test
  • /path/to/dir/*/:placeholder doesn't match /path/to/dir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug code to address defects in shipped code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: invalid rule (A path rule cannot contain anything after * token)

4 participants