Index: trac/mimeview/patch.py
===================================================================
--- trac/mimeview/patch.py	(revision 4006)
+++ trac/mimeview/patch.py	(working copy)
@@ -55,7 +55,8 @@
 
         add_stylesheet(req, 'common/css/diff.css')
         return Chrome(self.env).render_response(req, 'diff_div.html',
-                                                'text/html', data)
+                                                'text/html', data,
+                                                fragment=True)
 
     # Internal methods
 
Index: trac/wiki/web_ui.py
===================================================================
--- trac/wiki/web_ui.py	(revision 4006)
+++ trac/wiki/web_ui.py	(working copy)
@@ -396,18 +396,15 @@
         if page.name == 'WikiStart':
             data['title'] = ''
 
-        page_html = comment_html = attach_href = ''
-        latest_page = WikiPage(self.env, page.name)
-
+        comment_html = attach_href = ''
         if page.exists:
-            page_html = wiki_to_html(page.text, self.env, req, db)
+            latest_page = WikiPage(self.env, page.name)
             if version:
                 comment_html = wiki_to_oneliner(page.comment or '--',
                                                 self.env, db)
         else:
             if not req.perm.has_permission('WIKI_CREATE'):
                 raise HTTPNotFound('Page %s not found', page.name)
-            page_html = html.P('Describe "%s" here' % data['page_name'])
 
         # Show attachments
         attachments = attachments_data(self.env, req, db, 'wiki', page.name)
@@ -419,7 +416,6 @@
                      WikiSystem(self.env).get_pages(prefix)]
 
         data.update({'action': 'view',
-                     'page_html': page_html,
                      'comment_html': comment_html,
                      'latest_version': latest_page.version,
                      'attachments': attachments,
Index: trac/web/chrome.py
===================================================================
--- trac/web/chrome.py	(revision 4007)
+++ trac/web/chrome.py	(working copy)
@@ -36,12 +36,17 @@
                               format_time, http_date
 from trac.web.api import IRequestHandler, HTTPNotFound
 from trac.web.href import Href
-from trac.wiki import IWikiSyntaxProvider
+from trac.wiki import wiki_to_html, wiki_to_oneliner, IWikiSyntaxProvider
 
 def add_link(req, rel, href, title=None, mimetype=None, classname=None):
     """Add a link to the HDF data set that will be inserted as <link> element in
     the <head> of the generated HTML
     """
+    linkid = '%s:%s' % (rel, href)
+    linkset = req.environ.setdefault('trac.chrome.linkset', {})
+    if linkid in linkset:
+        return # Already added that link
+
     link = {'href': href}
     if title:
         link['title'] = title
@@ -49,9 +54,11 @@
         link['type'] = mimetype
     if classname:
         link['class'] = classname
-    # FIXME: don't add the same link more than once
-    req.environ.setdefault('trac.chrome.links', {}).setdefault(rel, []).append(link)
 
+    links = req.environ.setdefault('trac.chrome.links', {})
+    links.setdefault(rel, []).append(link)
+    linkset[linkid] = True
+
 def add_stylesheet(req, filename, mimetype='text/css'):
     """Add a link to a style sheet to the HDF data set so that it gets included
     in the generated HTML page.
@@ -61,22 +68,28 @@
         filename = filename[7:]
     else:
         href = Href(req.base_path).chrome
-    add_link(req, 'stylesheet', href(filename), mimetype=mimetype)
+    link = href(filename)
+    add_link(req, 'stylesheet', link, mimetype=mimetype)
 
 def add_script(req, filename, mimetype='text/javascript'):
     """Add a reference to an external javascript file to the template."""
+    scriptset = req.environ.setdefault('trac.chrome.scriptset', {})
+    if filename in scriptset:
+        return False # Already added that script
+
     if filename.startswith('common/') and 'trac.htdocs_location' in req.environ:
         href = Href(req.environ['trac.htdocs_location'])
         filename = filename[7:]
     else:
         href = Href(req.base_path).chrome
     script = {'href': href(filename), 'type': mimetype}
-    # FIXME: don't add the same script more than once
+
     req.environ.setdefault('trac.chrome.scripts', []).append(script)
+    scriptset[filename] = True
 
 def add_javascript(req, filename):
     """Deprecated: use `add_script()` instead."""
-    add_script(req, filename, mimetype='text/javascript')
+    return add_script(req, filename, mimetype='text/javascript')
 
 
 class INavigationContributor(Interface):
@@ -412,6 +425,10 @@
         data['format_time'] = partial(format_time, tzinfo=req.tz)
         data['fromtimestamp'] = partial(datetime.fromtimestamp, tz=req.tz)
 
+        # Wiki-formatting functions
+        data['wiki_to_html'] = partial(wiki_to_html, env=self.env, req=req)
+        data['wiki_to_oneliner'] = partial(wiki_to_html, env=self.env, req=req)
+
     def load_template(self, filename, method=None):
         """Retrieve a Template and optionally preset the template data.
 
@@ -428,7 +445,8 @@
 
         return self.templateloader.load(filename, cls=cls)
 
-    def render_response(self, req, template_name, content_type, data):
+    def render_response(self, req, template_name, content_type, data,
+                        fragment=False):
         """Render the `template_name` using the `data` for the context.
 
         The MIME `content_type` argument is used to choose the kind of template
@@ -438,15 +456,23 @@
         """
         if content_type is None:
             content_type = 'text/html'
-        doctype = {'text/html': DocType.XHTML_STRICT}.get(content_type)
         method = {'text/html': 'xhtml',
                   'text/plain': 'text'}.get(content_type, 'xml')
 
         template = self.load_template(template_name, method=method)
         self.populate_data(req, data)
-
         stream = template.generate(**data)
+        if fragment:
+            return stream
 
+        doctype = {'text/html': DocType.XHTML_STRICT}.get(content_type)
+        req.environ['trac.chrome.links'] = {}
+        req.environ['trac.chrome.scripts'] = []
+        data['chrome'].update({
+            'post_links': req.environ['trac.chrome.links'],
+            'post_scripts': req.environ['trac.chrome.scripts'],
+        })
+
         if method == 'text':
             return stream.render('text')
         else:
Index: templates/layout.html
===================================================================
--- templates/layout.html	(revision 4006)
+++ templates/layout.html	(working copy)
@@ -50,6 +50,14 @@
     <div id="main">
       ${select('*|text()')}
 
+      <script type="text/javascript" py:if="chrome.post_links">
+        <py:for each="link in chrome.post_links.get('stylesheet')">
+          $.loadStyleSheet("${link.href}", "${link.title}");
+        </py:for>
+      </script>
+      <script py:for="script in chrome.post_scripts"
+              type="${script.type}" src="${script.href}"></script>
+
       <div id="altlinks" py:if="chrome.links.alternate">
         <h3>Download in other formats:</h3>
         <ul>
Index: templates/wiki_view.html
===================================================================
--- templates/wiki_view.html	(revision 4007)
+++ templates/wiki_view.html	(working copy)
@@ -40,8 +40,13 @@
         </table>
       </py:if>
 
-      <div class="wikipage searchable">
-        $page_html
+      <div class="wikipage searchable" py:choose="">
+        <py:when test="page.exists">
+          ${wiki_to_html(page.text)}
+        </py:when>
+        <py:otherwise>
+          Describe ${page.name} here.
+        </py:otherwise>
       </div>
 
       ${list_of_attachments(attachments, attach_href, compact=True)}
