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

Equivalent of Next.js getServerSideProps in Aleph? #22

Closed
khromov opened this issue Nov 9, 2020 · 7 comments · Fixed by #45 or #404
Closed

Equivalent of Next.js getServerSideProps in Aleph? #22

khromov opened this issue Nov 9, 2020 · 7 comments · Fixed by #45 or #404
Labels
∆ feature New feature
Milestone

Comments

@khromov
Copy link

khromov commented Nov 9, 2020

I was reading the documentation about the useDeno hook and it says:

In Next.js, two functions called getStaticProps and getServerSideProps are used by the pages to fetch data at build time(SSR) or on each request. This solution isolates the data and view likes different roles of the back-end and front-end.

In Aleph.js, we prefer the mixed solution, a react hook called useDeno provided that allows you fetch data at build time(SSR) in a component with Deno runtime, that's more React Style likely:

After reading the examples, it appears you can only use the getStaticProps approach from Next (ie. fetching data at build time), but that there is no way to use the getServerSideProps approach?

A common use case for this that I don't understand how to solve in Aleph:

  • I have a site that needs to fetch some data locked behind a Bearer token. I want to pass this data as the initial props data to the component. I don't want this code in the frontend bundle.
  • I don't plan to hydrate this data on the frontend at all after initial load.

Is this use case not supported in Aleph? If not, it hampers the functionality to unusable levels for our projects. If it is indeed possible, can you please document how to use useDeno together with dynamic SSR that can fetch data on each request?

@ije
Copy link
Member

ije commented Nov 9, 2020

i'm planning to improve the useDeno hook in v0.3.0, will support dynamic SSR that can fetch data on each request.
like you say, currently alphejs will bundle the useDeno code even it's dead-code in browser, but this can be optimized by the new comilper in v0.3.0 .

@mohsenkhanpour
Copy link

It would be good to provide docs for class based components too.
getStaticProps, getServerSideProps would work in class based components too.
useDeno is a great idea, and less confusing than Next.js implementation.

@ije
Copy link
Member

ije commented Nov 9, 2020

@mohsenkhanpour i almost forgot how write a class component...but you are definitely right, should provide the gerStaticProps and getServerSideProps compatibility, and easier to migrate the next app to aleph. thanks 👍

@ije ije added the ∆ feature New feature label Nov 10, 2020
@ije ije added this to the v0.3.0 milestone Nov 15, 2020
@alfredosalzillo
Copy link
Contributor

Something like a withUseDeno hight order component should work.

const withUseDeno = (fn) => (Component) => (props) => {
     const ssrProps = useDeno(fn);
     return <Component {...ssrProps} {...props} />
}

class HelloDeno {
   render() {
     const { version } = this.props;
     return <p>Hello i'm Deno v{version}</p>
   }
}

export default withUseDeno(() => ({ version: Deno.version.deno }))(HelloDeno);

@ije
Copy link
Member

ije commented Nov 18, 2020

hi @alfredosalzillo, I like the HOC idea, are you interest in implementing it? or i will add it later, also withRouter for the useRouter hook, thanks

@alfredosalzillo
Copy link
Contributor

I'd like to contribute :)

@ije ije linked a pull request Nov 20, 2020 that will close this issue
@ije ije closed this as completed in #45 Nov 20, 2020
@ije ije reopened this Nov 20, 2020
@talentlessguy
Copy link
Member

IMO useDeno hook doesn't provide the main feature of Next.js getServerSideProps - access to req object

having it would be great

@ije ije closed this as completed in #404 Sep 23, 2021
ije added a commit that referenced this issue Sep 23, 2021
* Update types

* Allow `useDeno` and `ssr.props` access `Request` (close #22, #364, #401)

* Rename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
∆ feature New feature
Projects
None yet
5 participants