在SolidJS中使用SSG链接到具有不同参数的同一页面的方法是:

huangapple go评论61阅读模式
英文:

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 时,锚点标签将浏览器定向到不存在的页面。例如,使用 &lt;a&gt; 标签(使用SSG)从 /user/123 导航到 /user/456 会返回404错误。

我有两个解决404错误的方法,但两者都会导致SolidJS在点击链接时停止更新页面。

首先,使用 &lt;A&gt; 组件。

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 */
&lt;Routes path=&quot;/user&quot;&gt;
  &lt;Route path=&quot;/:id&quot; component={ User } /&gt;
&lt;/Routes&gt;

function User() {
  const params = useParams()
  return (
    &lt;OtherUsers url={ &quot;json/photo/&quot; + params.id } /&gt;
    /* More Components here */
  )
}

function OtherUsers(props) {
  const [users] = createResource(() =&gt; server.fetchData(props.url))
  return (
    &lt;&gt;
      &lt;Show when={ users() }&gt;&lt;Show when={ users()[0] }&gt;
        &lt;For each={ users() }&gt;
          {(num, key) =&gt; 
          &lt;img src={ num.photo } /&gt;
          &lt;a href={ &quot;/user/&quot; + num.id } &gt;{ num.name }&lt;/a&gt;
          /* More fetched content here */
          }
        &lt;/For&gt;
      &lt;/Show&gt;&lt;/Show&gt;
    &lt;/&gt;
  )
}

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 &lt;a&gt; 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 &lt;A&gt; component.

import { A } from &quot;@solidjs/router&quot;
...
&lt;A href={ &quot;/user/&quot; + num.id } &gt;{ num.name }&lt;/A&gt;

Second, the useNavigate component.

import { useNavigate } from &quot;@solidjs/router&quot;
const navigate = useNavigate()
...
navigate(&quot;/user/&quot; + 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 文件。以下是一些指南。

锚点标签

避免使用 &lt;a&gt; 锚点标签。它们会促使浏览器重新加载另一个 URL,从而导致 404 页面未找到错误。相反,使用 &lt;A&gt; 组件来动态更改路由。

资源信号和效果

避免使用 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 &lt;a&gt; anchor tags. They prompt your browser to reload another url causing a 404 page not found error. Instead, use the &lt;A&gt; 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&#39;t do this */
const [images] = createResource(() =&gt; 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(() =&gt; {
  server.fetchData(props.url).then(json =&gt; setImages(json))
})

huangapple
  • 本文由 发表于 2023年7月18日 00:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定