Rerender on new route or transition

AI-generated summary

In a Discourse development discussion titled ‘Rerender on new route or transition,’ users seek solutions for dynamically re-rendering DOM elements during page transitions. codechef reports difficulty updating a banner ad plugin via Widget or Component through a Plugin Outlet above the topic list, noting re-renders only occur on browser refresh. vinothkannans suggests most header UI sections don’t refresh on page transitions and requests code for review. jack2 proposes using a didTransition hook on the router with Ember.run.schedule('afterRender'), though notes it’s unreliable. daniyal and later daniyal clarify needing a footer slider component for anonymous users that updates per topic visit, removing visited topics — but mounting via footer outlet doesn’t trigger re-renders. Johani advises using the inserted-custom-html:footer event to update footer content dynamically, referencing a meta.discourse.org thread for details. Overgrow asks for best practices to dynamically render content at the top of the homepage on each transition, requesting example plugins. The core issue revolves around Discourse’s SPA behavior preventing automatic re-renders on route changes, requiring event-driven or lifecycle-based workarounds.

I’m building a banner ad plug-in. I have a section of the DOM that I need to re-render each time the page transitions. I have attempted (unsuccessfully) both a Widget and Ember Component, but neither are updating. I loaded them via a Plugin Outlet. I even tried to simplify the problem and dump the current timestamp of the load to the screen (rather than a banner), but only get a re-render if I refresh the browser.

Can someone please point me in the right direction for a solution? I’ve been through the main Discourse developer tutorials, BTW. Just seems like I’m missing something simple.

In which place you are trying to render? In Discourse most of the header UI sections will not refresh across page transactions unlike you hit browser refresh.

And can you post your code to see how you are doing it.

The plugin outlet right above the topic list.

Maybe you can try the following code (not tested):

// My plugin initializer
export default {

	name: 'myPlugin',

	initialize(container) {

		// Reopen the router to add a didTransition hook
		const router = container.lookup('router:main')
		router.reopen({
			doSomething: function() {
				// Wait for rendering to be over. THIS DOESN'T WORK IN ALL CASES. 
				// In fact, there is no way to be sure rendering is complete, because 
				// a 'complete rendering' is meaningless (think about a looping DOM  
				// animation)
				Ember.run.schedule('afterRender', null, () => {
					// Do your stuff here
					...
				})
			}.on('didTransition')
		})

	}

}

이 부분에 대해 더 자세히 안내해 주실 수 있는 분 계신가요?

무엇을 달성하고 싶으신지 설명해 주시겠어요?

푸터 영역에 컴포넌트를 추가하고, 사용자가 토픽을 방문할 때마다 페이지 로드 시 해당 컴포넌트의 내용을 업데이트해야 합니다. 이 컴포넌트는 커뮤니티의 인기 토픽을 포함하는 슬라이더이며, 익명 사용자에게만 토픽 페이지에서 표시됩니다. 따라서 사용자가 슬라이더에 포함된 각 토픽을 방문할 때마다 해당 슬라이드를 컴포넌트에서 제거해야 합니다. 아래 푸터 아웃렛을 통해 마운트해도 컴포넌트가 다시 렌더링되지 않습니다.

Discourse에서 "footer"가 렌더링될 때 앱이 "event"를 전송합니다.

해당 이벤트는 inserted-custom-html:footer입니다.

다음과 같이 해당 이벤트를 사용하여 푸터 내용을 업데이트할 수 있습니다.

api.onAppEvent("inserted-custom-html:footer", () => {
  // 여기에 JS를 추가하세요
});

여기에서 몇 가지 더 자세한 내용을 확인할 수 있습니다.

이 방법으로 원하는 결과를 얻을 수 있을 것입니다. 그렇지 않다면 알려주세요. 사용하고 있는 코드도 함께 게시해 주십시오.

홈페이지(오직) 상단에 동적 콘텐츠를 배치하고 싶습니다.

홈페이지로 전환될 때마다 다시 렌더링되도록 하는 가장 좋은 방법은 무엇인가요?

이 방식을 사용하는 기존 플러그인에 대한 링크를 제공해 주시면 감사하겠습니다.