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

policies and robots #2

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: ✨ policies page
  • Loading branch information
joshistoast committed Jun 15, 2023
commit 9a37ccc0584dd25e87faae2077713ab836c26183
32 changes: 32 additions & 0 deletions src/lib/server/data/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,35 @@ export const UPDATE_CART_BUYER = gql`
}
}
`

export const POLICY_CONTENT_QUERY = gql`
fragment Policy on ShopPolicy {
body
handle
id
title
url
}
query policy_query(
$language: LanguageCode
$privacyPolicy: Boolean!
$shippingPolicy: Boolean!
$termsOfService: Boolean!
$refundPolicy: Boolean!
) @inContext(language: $language) {
shop {
privacyPolicy @include(if: $privacyPolicy) {
...Policy
}
shippingPolicy @include(if: $shippingPolicy) {
...Policy
}
termsOfService @include(if: $termsOfService) {
...Policy
}
refundPolicy @include(if: $refundPolicy) {
...Policy
}
}
}
`
42 changes: 42 additions & 0 deletions src/routes/[[locale]]/policies/[handle]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { PageServerLoad } from './$types'
import { POLICY_CONTENT_QUERY } from '$lib/server/data'
import invariant from 'tiny-invariant'
import { error } from '@sveltejs/kit'

export const load: PageServerLoad = async ({ params, locals }) => {
const { storefront, locale } = locals
const { handle } = params

const policyName = handle.replace(
/-([a-z])/g,
(_: unknown, m1: string) => m1.toUpperCase(),
) as 'privacyPolicy' | 'shippingPolicy' | 'termsOfService' | 'refundPolicy'

const { data } = await storefront.query({
query: POLICY_CONTENT_QUERY,
variables: {
privacyPolicy: false,
shippingPolicy: false,
termsOfService: false,
refundPolicy: false,
[policyName]: true,
language: locale.language,
}
})

invariant(data, 'No data returned from Shopify API')

const policy = data.shop?.[policyName]

if (!policy) {
throw error(404, 'Policy not found')
}

return {
seo: {
title: policy.title,
description: policy.body,
},
policy,
}
}
9 changes: 9 additions & 0 deletions src/routes/[[locale]]/policies/[handle]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import type { PageServerData } from './$types'

export let data: PageServerData
$: ({ policy } = data)
</script>

<h1>{policy.title}</h1>
<div>{@html policy.body}</div>