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

[Feature Request]use alova in sveltekit #133

Closed
JOU-amjs opened this issue Jun 27, 2023 · 2 comments
Closed

[Feature Request]use alova in sveltekit #133

JOU-amjs opened this issue Jun 27, 2023 · 2 comments
Labels
bug:confirmed Something isn't working released Svelte Issues or pull request for Svelte

Comments

@JOU-amjs
Copy link
Contributor

JOU-amjs commented Jun 27, 2023

I am trying to use alova with SvelteKit (not Svelte). The YouTube vid promoting it, as well as the website, indicates is support SSR, which would indicate it is possible to run server side with SvelteKit.

SvelteKit 1 uses directory based routing (similar to Next.JS), and any route would typically have at least 2 pages:

  • /folder-route-name/+page.js (runs server side first, then client side afterwards)
  • OR
  • /folder-route-name/+page.server.js (if wanting to run server only, never in client, as perhaps secret API key involved which should not be exposed to client)
  • /folder-route-name/+page.svelte (UI code, html, css, client side javascript)

A typical +page.js uses an extend web standard fetch (that runs in node.js too, i.e. server side first). Either +page.js file needs to "return data" object which is then accessible in the +page.svelte file.

Example:

// ------- +page.js ------------
let query = `
query homepage {
  homepageSliders: sliders {
    nodes {
      id
      title
      sliderData {
        image {
          id
          mediaItemUrl
        }
        buttonLabel
        buttonLink
      }
    }
  }
}
`;

export const load = async ({ fetch, event, setHeaders }) => {
	try {
		const response = await fetch(import.meta.env.VITE_GRAPHQL_URL, {
			method: 'POST',
			headers: { 'Content-Type': 'application/json' },
			body: JSON.stringify({ query })
		});
		setHeaders({
			'cache-control': `max-age=0, s-maxage=${60 * 60}`
		});
		const { data } = await response.json();
		return data;
	} catch (error) {
		console.error(`Error in load function for: ${error}`);
	}
};

...Note the 'data' from the API call must be returned. Then in +page.svelte, you'd have a script tag at the top that would make the data object available in the rest of the page, something like:

<!-- ---- +page.svelte ----- -->
<script>
export let data;
</script>

<pre>
My Homepage data: {JSON.stringify(data), null, 2}
</pre>

...basically anything in braces lets you access the data object or other javascript you've written in the script tags.

I followed your basic svelte example listed above, but can not figure out where to return the data. Each place I try it results in an error, which means the data from the API call is never available in the UI building file.

I take it you aren't familiar with SvelteKit, but perhaps I've given enough data that you might have an idea how to covert the code example referenced in your above link into a function the in the end will 'return data'?

Originally posted by @trulycool in #49 (comment)

@JOU-amjs JOU-amjs changed the title use alova in sveltekit [Feature Request]use alova in sveltekit Jun 27, 2023
JOU-amjs pushed a commit that referenced this issue Jun 28, 2023
# [2.8.0](v2.7.1...v2.8.0) (2023-06-28)

### Features

* **logger:** now it will log hit cache when response cache is hit ([9ccdc82](9ccdc82))
* **optimization:** initial loading value is true when request immediately ([4b52b5a](4b52b5a)), closes [#108](#108)
* **ssr:** do not send request any more when use hooks in ssr ([cfcfa69](cfcfa69)), closes [#133](#133)
@JOU-amjs
Copy link
Contributor Author

🎉 This issue has been resolved in version 2.8.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@JOU-amjs
Copy link
Contributor Author

JOU-amjs commented Jun 28, 2023

Now alova is optimized in SSR. and docs is here.

This issue is refer this post.

@JOU-amjs JOU-amjs added bug:confirmed Something isn't working Svelte Issues or pull request for Svelte labels Jul 27, 2023
Codechief09 pushed a commit to Codechief09/vue-alova that referenced this issue Oct 13, 2023
# [2.8.0](alovajs/alova@v2.7.1...v2.8.0) (2023-06-28)

### Features

* **logger:** now it will log hit cache when response cache is hit ([9ccdc82](alovajs/alova@9ccdc82))
* **optimization:** initial loading value is true when request immediately ([4b52b5a](alovajs/alova@4b52b5a)), closes [#108](alovajs/alova#108)
* **ssr:** do not send request any more when use hooks in ssr ([cfcfa69](alovajs/alova@cfcfa69)), closes [#133](alovajs/alova#133)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:confirmed Something isn't working released Svelte Issues or pull request for Svelte
Projects
None yet
Development

No branches or pull requests

1 participant