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

how to get current pathname in server component? #84

Closed
himself65 opened this issue Jun 26, 2023 · 7 comments · Fixed by #124
Closed

how to get current pathname in server component? #84

himself65 opened this issue Jun 26, 2023 · 7 comments · Fixed by #124

Comments

@himself65
Copy link
Contributor

himself65 commented Jun 26, 2023

'use server';

const App = () => {
  // Does this possible
  const pathname = usePathname()
}
@dai-shi
Copy link
Owner

dai-shi commented Jun 26, 2023

That's my next action item:

Support interacting with server (req and res)

Stay tuned!

@himself65
Copy link
Contributor Author

My workaround:

const AppCreator = (pathname: string) => (props): ReactElement => {
  return (
    <div className="flex flex-col-reverse sm:flex-row">
      <nav className="w-full sm:w-64">
        <Sidebar />
      </nav>
      <main className="flex-1 p-6 w-full sm:w-[calc(100%-16rem)]">
        {/*<Editor />*/}
      </main>
    </div>
  );
};
export default defineRouter(
  async id => {
    switch (id) {
      case 'index': {
        const { default: AppCreator } = await import('./src/app.js');
        return AppCreator(id)
      }
      default:
        return null;
    }
  },
  async () => {
    return ['index'];
  }
);

@himself65
Copy link
Contributor Author

My workflow is to implement a document generator using waku (as you can see the code in https://github.com/toeverything/AFFiNE/tree/master/apps/docs).

So the logics are

  1. get all possible paths
  2. read the current markdown file from current path
  3. render to the static RSC

@dai-shi
Copy link
Owner

dai-shi commented Jun 26, 2023

My workaround:

I think that works too.

@dai-shi
Copy link
Owner

dai-shi commented Jun 27, 2023

I thought something like #86 might help, but it actually doesn't because of caching.
I think returning a different component should be a canonical solution.
A little worried with your solution as it creates a new component every time, but it wouldn't be a big issue.

@lubieowoce
Copy link

isn't it possible to just pass a prop to the element in the router definition and thus avoid recreating the component? I did something like that here a while ago

https://github.com/lubieowoce/tangle/blob/9059cc35d75ab8c6e643df250275f06ee266c387/examples/router-in-waku/entries.ts#L67

@dai-shi
Copy link
Owner

dai-shi commented Jul 1, 2023

Yeah, that's possible too. It's almost like making getEntry a component rather than a function.

Do you know how bad is (path) => () => ...?

@dai-shi dai-shi mentioned this issue Jul 31, 2023
dai-shi added a commit that referenced this issue Sep 10, 2023
dai-shi added a commit that referenced this issue Sep 11, 2023
* wip: examples 01 dev

* hack with _ssr, not ideal though

* examples 02

* simplify waku/client usage

* example 03

* example 04

* improve example 04

* examples 05

* example 06

* fix example 06

* support children

* wip: waku/router, which is tough one

* rename from Server to Slot

* wip: fix router

* refactor consistent names

* wip: fix router

* add react hooks eslint plugin

* improve prefetched

* fix example 08

* fix example 09

* fix test

* wip: ssr config

* fix ex 06

* fix waku/router

* wip: website

* fix interleaveHtmlSnippets

* remove unstable_rootDir

* fix router build

* fix explicit index path (suboptimal?)

* this should be a proper fix

* website: use path prop #84

* fix action and improve example/06

* re-design router input string and allow client cache control

* update website

* make initialInput prop optional

* use "" for initial input for simplicity

* fix and a workaround for empty input string, seems suboptimal..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants