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

RFC: getInitialProps/getClientSideProps #364

Closed
hazae41 opened this issue Aug 24, 2021 · 8 comments
Closed

RFC: getInitialProps/getClientSideProps #364

hazae41 opened this issue Aug 24, 2021 · 8 comments
Labels
☼ user feedback wanted Wait for user feedback

Comments

@hazae41
Copy link
Member

hazae41 commented Aug 24, 2021

IMHO getInitialProps is very useful but complex to re-implement properly and to bundle properly

What I suggest is getServerSideProps, which we already have, combined with a new function named getClientSideProps.

This function would generate the props client-side, just like getInitialProps, but without being compatible with the server

  • First page load:
    getServerSideProps is called
  • When the user navigates:
    getClientSideProps is called

This way, no request to the server is made each time the user clicks somewhere, especially if the client already has the props in a cache (e.g. swr)

If getClientSideProps is NOT defined, then a request is made and getServerSideProps is called server-side (current behaviour)

The bundling of those two functions is easier than getInitialProps, and their behavior is more understandable and less error-prone

@hazae41
Copy link
Member Author

hazae41 commented Aug 24, 2021

The problem is that useDeno is a hook that only accepts one function (getServerSideProps/getStaticProps)

@shadowtime2000 shadowtime2000 added the ☼ user feedback wanted Wait for user feedback label Aug 24, 2021
@talentlessguy
Copy link
Member

there's ssr now, I don't think getServerSideProps makes sense anymore

for example:

export const ssr: SSROptions = {
  props: async ({ params }) => {
    const res = await fetch(`http://example.com`)

    if (res.status !== 200) {
      console.log(await res.text())
      return {}
    }

    const json = await res.json()

    return json as WalletData
  },
  paths: () => {
    return []
  }
}

@ije
Copy link
Member

ije commented Sep 20, 2021

what if we allow to access the Req in useDeno hook?, for example:

export default function Page() {
  const isLogined = useDeno(req => {
    return req.headers.get('Auth') === 'XXX'
  }, { revalidate: true })

  return (
    <p>isLogined: {isLogined}</p>
  )
}

with ssr.props options:

export const ssr: SSROptions = {
  props: req => {
    return {
       $revalidate: true,
       username: req.params.username,
       logined: req.headers.get('Auth') === 'XXX'
    }
  }
}

@talentlessguy
Copy link
Member

@ije second options makes more sense since it already exposes request parameters

@hazae41
Copy link
Member Author

hazae41 commented Sep 20, 2021

The useDeno hook is very clean to use

@hazae41
Copy link
Member Author

hazae41 commented Sep 20, 2021

@ije second options makes more sense since it already exposes request parameters

We can put params in req

@ije
Copy link
Member

ije commented Sep 20, 2021

updated, and rename revalidate to ttl

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
@ije
Copy link
Member

ije commented Sep 24, 2021

added in beta.18

@ije ije closed this as completed Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☼ user feedback wanted Wait for user feedback
Projects
None yet
Development

No branches or pull requests

4 participants