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

support nested routes, and a base prop to start from if necessary #100

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

sskoopa
Copy link

@sskoopa sskoopa commented Dec 11, 2016

No description provided.

@developit
Copy link
Member

This is awesome @sskoopa! Only thought I had was maybe use props.base directly and skip persisting it into state.baseUrl. Think that'd work?

@sskoopa
Copy link
Author

sskoopa commented Dec 12, 2016

I was just going on the fact that base is a calculated field, and props are supposed to be immutable in react-like codebases, no?

We need to calculate it before setting the context in getChildContext(), as that method is not passed the context.

@sskoopa
Copy link
Author

sskoopa commented Dec 12, 2016

Updated to use props.base, tests still pass

@developit
Copy link
Member

Ack! Sorry, I hadn't meant to mutate this.props heh. I figured something like:

class Router extends Component {
	constructor(props, context) {
		super(props);
		let base = props.base || '';
		if (props.history) {
			customHistory = props.history;
		}
		if (context && context[CONTEXT_KEY]) {
			base = context[CONTEXT_KEY] + base;
		}

		this.state = {
			url: this.props.url || getCurrentUrl()
		};
	}

	getChildContext() {
		let base = this.props.base || '';
		if (context && context[CONTEXT_KEY]) {
			base = context[CONTEXT_KEY] + base;
		}
		return { [CONTEXT_KEY]: base };
	}
}

@sskoopa
Copy link
Author

sskoopa commented Dec 12, 2016

Once base is calculated, where do you store it to use in getMatchingChildren?

@developit
Copy link
Member

Could just leave it as this.base - the reason to skip passing it into state was to avoid a re-render.

@sskoopa
Copy link
Author

sskoopa commented Dec 12, 2016

I'll work that direction then

@sskoopa
Copy link
Author

sskoopa commented Dec 12, 2016

s/this.base/this.baseUrl works, of course this.base is already a preact variable :)

@developit
Copy link
Member

Ahhh haha I completely missed that

src/index.js Outdated

this.state = {
url: this.props.url || getCurrentUrl()
};
}

getChildContext() {
return {CONTEXT_KEY: this.baseUrl};
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be [CONTEXT_KEY].

@stonecobra
Copy link

stonecobra commented Dec 19, 2016

ok, I now:

  • have a component that can pass context deeper if needed
  • Router works with path attribute now, so will work just inside a Router
  • updated DOM tests to check nested support for default, path, and
  • context baseUrl now handles variable match paths as well
  • updated README example

Let me know if you think anything else should be there.

super(props);
this.baseUrl = this.props.base || '';
if (props.path) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we need to move this into the if block on L156?

@matannoam-zz
Copy link

+1

@developit
Copy link
Member

this is high on my todo list, we're using workarounds right now and it's ugly.

@ashsearle ashsearle mentioned this pull request Feb 6, 2017
@osdevisnot
Copy link

osdevisnot commented Feb 17, 2017

+1. Planning to switch our app to preact-router once nested routes are in.

@developit
Copy link
Member

Yes - sorry for dropping this on the floor, we're super keen to get this merged too since our apps are using a workaround right now.

@sskoopa
Copy link
Author

sskoopa commented Mar 4, 2017

@developit I updated the PR to remove conflicts, ready to merge

@developit
Copy link
Member

Awesome, thanks a bunch! Hopefully we can merge this, right now we are hacking around it haha.

@developit
Copy link
Member

I tried and failed to resolve conflicts, didn't want to botch it.

@sskoopa
Copy link
Author

sskoopa commented Apr 18, 2017

@developit, I merged everything the way I think it should, but one of the tests I wrote for Match is failing:

FAILED TESTS:
  dom
    Router
      ✖ should support nested routers with Match
        Chrome 57.0.2987 (Mac OS X 10.12.4)
      TypeError: props.children[0] is not a function
          at Match.render (test/dom.js:1608:49)
          at renderComponent (test/dom.js:978:39)
          at renderComponent (test/dom.js:990:26)
          at Router.forceUpdate (test/dom.js:1100:14)
          at Router.routeTo (test/dom.js:1337:9)
          at routeTo (test/dom.js:1204:19)
          at route (test/dom.js:1189:10)
          at Context.<anonymous> (test/dom.js:458:20)

I left the console.log of children in Match.render as the child is a vNode, and usually the Router did a cloneElement on match. Do we need cloneElement on Match.render()? If so, do we need more or other tests on Match itself?

test/dom.js Outdated
<Router base='/ccc'>
<X path="/jjj" />
<Match path="/xxx/:bar*">
<Router>
Copy link
Author

Choose a reason for hiding this comment

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

While the other nested router tests pass, the Match here cannot render the child as it is a vNode.

Copy link
Member

Choose a reason for hiding this comment

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

hmmm. yeah we might need to do type detection in Match. I realize now we sortof have two matches, so it might be good to merge them. Need to think on this for sure, because I like the options your Match gives us.

@sskoopa
Copy link
Author

sskoopa commented Apr 24, 2017

OK, Match components are merged and tests pass. I think more people will use the simple Match, but the power is there. gzip size 2050 (2K+2). I could shorten the CONTEXT_KEY constant name :)

@developit developit mentioned this pull request Jul 17, 2017
@marcodeltongo
Copy link

This would be super-useful to avoid workarounds, anything missing for approval?

@chemzqm
Copy link

chemzqm commented Aug 18, 2017

Any update on this? My boss want to use sub directory according to this article https://fly.io/articles/one-hostname-to-rule-them-all/

@eligao
Copy link

eligao commented Sep 4, 2017

Hi, anything I could help with this? Would be nice to have it.

@laggingreflex laggingreflex mentioned this pull request Oct 5, 2017
@jahilldev
Copy link

@sskoopa @developit Any news on this? Really don't want the extra size of React Router as this does everything I need. Thanks

@developit
Copy link
Member

If anyone wants to take a stab at resolving the conflicts that'd help. We all want it merged :)

@stevengrimaldo
Copy link

stevengrimaldo commented Oct 21, 2018

When I go to review the PR i'm not seeing any conflicts, is there a certain view I should be looking at?

I'm also very interested in adding the ability for nested routes, how can i help @developit?

@matannoam-zz matannoam-zz mentioned this pull request Nov 9, 2018
@matannoam-zz
Copy link

Hi quickly updated - #293

@hbroer
Copy link

hbroer commented Mar 13, 2019

This would be so nice to see it merged. Big thumbs up to @sskoopa.
@developit Is there any change to see this merged? Can i do something like local merge and manual testing maybe?

@marvinhagemeister
Copy link
Member

@hbroer Agree the PR is really great and in my opinion there is not much standing in the way of a merge. The PR needs to be updated against the latest changes in master for Preact X, but that seems to be all that's left to do 🎉

silentsakky added a commit to silentsakky/preact-router that referenced this pull request Sep 18, 2019
silentsakky added a commit to silentsakky/preact-router that referenced this pull request Sep 18, 2019
@Haugen
Copy link

Haugen commented Sep 15, 2020

What happened to this feature? I can't seam to find that it was solved and merged in another issue. Is this still being considered or have it been discarded?

I would be happy to try and help solve the merge conflicts, but I'm not sure about the proper way to do it. Can I even update this PR?

@developit
Copy link
Member

developit commented Oct 24, 2020

@Haugen it's still being considered, just hasn't been prioritized. There are so many great routers available that work with Preact, but the drawback is that it's hard to justify spending time on new features for preact-router. FWIW I would really like to get this functionality integrated, I just can't take that time away from current projects.

FWIW, @38elements recently contributed a guide section in the README that covers how nested Routers work today (without the changes in this PR): https://github.com/preactjs/preact-router#nested-routers

@renanbandeira
Copy link

@developit what is missing for that to be merged? I could help investing time on it. I understand there are many routers that work with Preact, but since I'm working with preact for performance reasons, I would like to have this base prop to handle my requests on a subdir (it's not about the nested requests, it's about the base prop)

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

Successfully merging this pull request may close these issues.

None yet