英文:
How to link to the same page with a different parameter in SolidJS using SSG
问题
我在努力让我的SolidJS应用使用静态站点生成(Static Site Generation,SSG)来链接用户页面,但遇到了困难。
/* App.jsx */
<Routes path="/user">
<Route path="/:id" component={User} />
</Routes>
function User() {
const params = useParams();
return (
<OtherUsers url={"json/photo/" + params.id} />
/* 更多组件在这里 */
);
}
function OtherUsers(props) {
const [users] = createResource(() => server.fetchData(props.url));
return (
<>
<Show when={users()}>
<Show when={users()[0]}>
<For each={users()}>
{(num, key) =>
<img src={num.photo} />
<a href={"/user/" + num.id}>{num.name}</a>
/* 更多获取的内容在这里 */
}
</For>
</Show>
</Show>
</>
);
}
这在开发环境 npm run dev
中按预期工作。例如,我可以从 /user/123
导航到 /user/456
。然而,当构建为静态站点生成(SSG)网站 npm run build
时,锚点标签将浏览器定向到不存在的页面。例如,使用 <a>
标签(使用SSG)从 /user/123
导航到 /user/456
会返回404错误。
我有两个解决404错误的方法,但两者都会导致SolidJS在点击链接时停止更新页面。
首先,使用 <A>
组件。
import { A } from "@solidjs/router";
// ...
<A href={"/user/" + num.id}>{num.name}</A>
其次,使用 useNavigate
组件。
import { useNavigate } from "@solidjs/router";
const navigate = useNavigate();
// ...
navigate("/user/" + num.id, { replace: true });
这两个解决方案都会更新浏览器中的URL,并且params.id
似乎会更新。然而,User()
和 OtherUsers()
不会触发,因此页面上的任何内容都不会重新绘制。
如何强制页面重新绘制?
英文:
I am struggling to get my SolidJS app to link user pages together using Static Site Generation (SSG).
/* App.jsx */
<Routes path="/user">
<Route path="/:id" component={ User } />
</Routes>
function User() {
const params = useParams()
return (
<OtherUsers url={ "json/photo/" + params.id } />
/* More Components here */
)
}
function OtherUsers(props) {
const [users] = createResource(() => server.fetchData(props.url))
return (
<>
<Show when={ users() }><Show when={ users()[0] }>
<For each={ users() }>
{(num, key) =>
<img src={ num.photo } />
<a href={ "/user/" + num.id } >{ num.name }</a>
/* More fetched content here */
}
</For>
</Show></Show>
</>
)
}
This work as expected in the development environment npm run dev
. For example, I can navigate from /user/123
to /user/456
.
However when constructed as a Static Site Generation (SSG) website npm run build
, the anchor tag directs the browser to a page that doesn't exist. For example, /user/456
returns a 404 error when navigating from /user/123
using the <a>
tag (using SSG).
I have two solutions to fix the 404 error - but the both cause SolidJS to stop updating the page when the link is clicked.
First, the <A>
component.
import { A } from "@solidjs/router"
...
<A href={ "/user/" + num.id } >{ num.name }</A>
Second, the useNavigate
component.
import { useNavigate } from "@solidjs/router"
const navigate = useNavigate()
...
navigate("/user/" + num.id, { replace: true })
Both of these solutions update the url in the browser and the params.id
appears to update. However User()
and OtherUsers()
are not triggered and so nothing on the page is re-drawn.
How do I force the page to re-draw?
答案1
得分: 1
然而,当构建为静态站点生成(SSG)网站时,锚点标签将浏览器引导到一个不存在的页面。
您如何提供您的SSG站点?您可能需要设置重定向。
对于您来说,使用哈希路由集成是否可行?
https://docs.solidjs.com/guides/how-to-guides/routing-in-solid/solid-router#hash-mode-router
英文:
> However when constructed as a Static Site Generation (SSG) website, the anchor tag directs the browser to a page that doesn't exist
How do you serve your SSG site? You may need to setup redirects.
Is it viable for you to use the Hash router integration?
https://docs.solidjs.com/guides/how-to-guides/routing-in-solid/solid-router#hash-mode-router
答案2
得分: 0
你可以使用 SolidJS 来创建一个静态站点生成(SSG)应用,而无需设置复杂的服务器。应用程序导航必须始终从 index.html
开始,因为在运行 npm run build
时,这是唯一生成的 HTML 文件。以下是一些指南。
锚点标签
避免使用 <a>
锚点标签。它们会促使浏览器重新加载另一个 URL,从而导致 404 页面未找到错误。相反,使用 <A>
组件来动态更改路由。
资源信号和效果
避免使用 SolidJS 的 createResource
组件。当其父组件的属性更改时,它不会重新触发。
/* 不要这样做 */
const [images] = createResource(() => server.fetchData(props.url))
相反,使用 createEffect
来在父组件的属性更改时触发,使用 createSignal
为您希望重新绘制的 DOM 元素的数据。
/* 这样做 */
const [images, setImages] = createSignal([])
createEffect(() => {
server.fetchData(props.url).then(json => setImages(json))
})
英文:
You can use SolidJS to create an Static Site Generation (SSG) app without setting up a complex server. App navigation must always start at index.html
because this is the only html file generated when running npm run build
. Here are some guidelines.
Anchor Tags
Avoid using <a>
anchor tags. They prompt your browser to reload another url causing a 404 page not found error. Instead, use the <A>
component to to dynamically change the route.
Resources Signals and Effects
Avoid using the SolidJS createResource
component. It does not re-trigger when the properties of it's parent component change.
/* Don't do this */
const [images] = createResource(() => server.fetchData(props.url))
Instead, use createEffect
to trigger whenever the properties of the parent component change and createSignal
for the data of the DOM elements that you want re-drawn.
/* Do this */
const [images, setImages] = createSignal([])
createEffect(() => {
server.fetchData(props.url).then(json => setImages(json))
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论